我试着让下面的插入符号在我的下拉菜单上旋转180度。在我正在尝试实现的解决方案中,它在单击时将插入符号的类更改为向上切换或向下切换。我第一次点击它时,它会向上旋转,第二次点击时,它会立即回到它的起始位置,然后再旋转回来。我闻到了脏代码的味道,添加这个切换旋转动画的最简单的方法是什么?提前感谢您的帮助。
下面是我目前的css:
.toggle-up {
animation-name: toggle-up;
animation-delay: 0.25s;
animation-duration: 0.75s;
animation-fill-mode: forwards;
}
.toggle-down {
animation-name: toggle-down;
animation-delay: 0.25s;
animation-duration: 0.75s;
animation-fill-mode: forwards;
}
/*animations*/
@keyframes toggle-up {
100% {
transform: rotate(180deg);
}
}
@keyframes toggle-down {
100% {
transform: rotate(180deg);
}
}

发布于 2015-06-29 14:16:43
对于这么简单的东西,你并不真的需要关键帧动画。如果你只是在你的图标上添加一个类,然后删除它,这将应用你的旋转。Here is a working plunkr using font awesome and a simple rotation。这只是一个简单的例子,您需要使用供应商前缀,并且要知道css转换在较旧的浏览器中不起作用。
<div id="container">
<i id="icon" class="fa fa-arrow-down"></i>
</div>
.fa-arrow-down{
transform: rotate(0deg);
transition: transform 1s linear;
}
.fa-arrow-down.open{
transform: rotate(180deg);
transition: transform 1s linear;
}
(function(document){
var div = document.getElementById('container');
var icon = document.getElementById('icon');
var open = false;
div.addEventListener('click', function(){
if(open){
icon.className = 'fa fa-arrow-down';
} else{
icon.className = 'fa fa-arrow-down open';
}
open = !open;
});
})(document);发布于 2015-06-29 14:16:52
.square {
width: 100px;
height: 100px;
background-color: #ff0000;
transition: all 0.75s 0.25s;
}
.toggle-up {
transform: rotate(180deg);
}
.toggle-down {
transform: rotate(0);
}您应该具有初始状态才能完成动画。
下面是示例:codepen
更新
以下是不使用javascript的版本:codepen
<label for="checkbox">
<input type="checkbox" id="checkbox">
<div class="square toggle-down"></div>
</label>
#checkbox {
display: none;
}
.square {
width: 100px;
height: 100px;
background-color: #ff0000;
transition: all 0.75s 0.25s;
transform: rotate(0);
}
#checkbox:checked + .square {
transform: rotate(180deg);
}一般的想法是使用Adjacent sibling selectors和复选框checked state来更改块类。
https://stackoverflow.com/questions/31108800
复制相似问题