html - :not does not work for animation on uncheck

标签 html css

我创建了一个小切换开关。我使用隐藏的复选框和以下 CSS

input[type=checkbox]:checked ~ div {
  animation-name: slide;
  animation-duration: 350ms;
  margin-left: 32px;
}

和 HTML

<input type="checkbox" id="checkbox" />
<div class="knob">
  <div class="knob inside"> | </div>
</div>

完整代码笔:https://codepen.io/anon/pen/eZKyzK

而且很明显,以下是行不通的:

input[type=checkbox]:not(:checked) ~ div {
  animation-name: slide;
  animation-duration: 350ms;
  animation-direction: reverse;
  margin-left: 32px;
}

因为进入状态当然是:not(:checked)。但我不想让动画开始。第二个问题是动画只会播放一次。有没有办法实现开关行为: 1.跳过第一个动画 2. 每次复选框状态改变时播放动画,而不是只播放一次

最佳答案

  1. 每次复选框状态改变时播放动画

添加了一个新的倒车动画。它确实有效。

@keyframes slideBack {
    from {
      margin-left: 32px;
    }
    to {
      margin-left: 0px;
    }
}

同样在您分享的 codepen 链接中,请更改此链接,

input[type=checkbox]:not(:checked) ~ div {
  animation-name: slideBack;
  animation-duration: 350ms;
  margin-left: 0px;
}

关于html - :not does not work for animation on uncheck,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36736918/

相关文章:

javascript - 切换 innerHTML

html - 从 jquery-ui.css 更改 css

javascript - bootstrap 不在 iframe 中显示移动布局

jquery - 如何在不拉伸(stretch)的情况下将所有图像设置为等宽和等高

html - 如何在媒体查询样式表旁边附加单独的样式表

html - 在 css3 中使用 font-face 更改外语字体?

javascript - 在页面加载时隐藏一个div,然后在单击按钮时取消隐藏

javascript - 需要多次使用 getElementById 来更改图像 src

php - 文本文件 - 定义占位符,通过 PHP 读取 - 在网站上生成 HTML/CSS

javascript - 如何使用javascript获取浏览器最后一次请求加载的文件列表?