【发布时间】:2016-05-12 00:27:08
【问题描述】:
动画总是在元素从 display:none 到显示状态时播放。 如果我想设置自定义复选框的样式。我只需要它在用户选中或取消选中它时播放动画。
input[type=checkbox] { opacity: 0; } // native checkbox hidden
input[type=checkbox] ~ .mycheck {
animation: uncheck 300ms ease-out forwards; // play uncheck animate
}
input[type=checkbox]:checked ~ .mycheck {
animation: check 300ms ease-out forwards; // play checked animate
}
问题是check 和uncheck 不仅在用户点击它时播放。它也在页面加载时播放。或者当父元素切换隐藏到显示时。例如在引导选项卡中。
我知道 javascript 可以轻松处理它。但实际上,这个问题来自一个感兴趣的github项目https://github.com/FezVrasta/bootstrap-material-design/issues/48。那个帅哥想用bootstrap3实现google material design。
目前我能想到一个方法(不完美)。当:not(:hover) 设置animation-duration: 1ms;。让动画快速结束。但是用户还是会看短视频。
input[type=checkbox]:not(:hover) ~ .mycheck {
animation-duration: 1ms;
}
无法设置为 0 毫秒。否则动画在用户悬停之前不会播放。
有没有更好的办法?
【问题讨论】:
标签: twitter-bootstrap css animation css-animations bootstrap-material-design