有没有办法用javascript完全控制CSS动画?我需要通过单击按钮来更改动画的状态。
@keyframes example {
0% {transform: translate(0,0);}
29% {transform: translate(0,0);}
33% {transform: translate(-100px,0);}
62% {transform: translate(-100px,0);}
66% {transform: translate(-200px,0);}
96% {transform: translate(-200px,0);}
100% {transform: translate(0,0);}}
当点击按钮时,是否有可能将动画状态更改为50%?
我想做一个滑块与最小的js,但需要处理这个小点。
如果没有办法做到这一点,你能告诉我怎么用不同的方法吗?
#box
{
width:100px;
height:100px;
overflow:hidden;
position:relative;
}
#all
{
width:300px;
height:100px;
animation-name: example;
animation-duration: 13.5s;
animation-iteration-count: infinite;
}
.box1
{
width:100px;
height:100px;
background-color:red;
float:left;
}
.box2
{
width:100px;
height:100px;
background-color:blue;
float:left;
}
.box3
{
width:100px;
height:100px;
background-color:green;
float:left;
}
@keyframes example {
0% {transform: translate(0,0);}
29% {transform: translate(0,0);}
33% {transform: translate(-100px,0);}
62% {transform: translate(-100px,0);}
66% {transform: translate(-200px,0);}
96% {transform: translate(-200px,0);}
100% {transform: translate(0,0);}
}
@keyframes example2 {
0% {background-color:black;}
29% {background-color:black;}
33% {background-color:white;}
62% {background-color:white;}
66% {background-color:white;}
96% {background-color:white;}
100% {background-color:black;}
}
@keyframes example3 {
0% {background-color:white;}
29% {background-color:white;}
33% {background-color:black;}
62% {background-color:black;}
66% {background-color:white;}
96% {background-color:white;}
100% {background-color:white;}
}
@keyframes example4 {
0% {background-color:white;}
29% {background-color:white;}
33% {background-color:white;}
62% {background-color:white;}
66% {background-color:black;}
96% {background-color:black;}
100% {background-color:white;}
}
.circle
{
width:10px;
height:10px;
background-color:white;
border-radius:5px;
float:left;
margin-left:10px;
}
.circle1
{
animation-name: example2;
animation-duration: 13.5s;
animation-iteration-count: infinite;
}
.circle2
{
animation-name: example3;
animation-duration: 13.5s;
animation-iteration-count: infinite;
}
.circle3
{
animation-name: example4;
animation-duration: 13.5s;
animation-iteration-count: infinite;
}
#circles
{
position:absolute;
bottom:10px;
left:15px;
}
span
{
color:white;
margin-top:20px;
display:block;
text-align:center;
}
<div id="box">
<div id="all">
<div class="box1"><span>fdsaf</span></div><div class="box2"><span>fdsafd</span></div><div class="box3"><span>fdsafdsaf</span></div>
</div>
<div id="circles"><div class="circle circle1"></div><div class="circle circle2"></div><div class="circle circle3"></div></div>
</div>
发布于 2016-10-05 04:01:24
我将在这里冒险说“不”,尽管这在某种奇怪的方式下在技术上可能是可行的。此外,从超级书呆子程序员的角度来看,这违反了关注点分离(有些人不关心,我知道我知道),也是一件非常奇怪的事情。
也就是说,我的rec是在CSS中做所有的事情(即点击按钮添加/切换一个类,这会触发转换或动画)。这样,你就可以有一堆不同的类来为不同的状态或按钮按下或其他事件触发不同的动画。
相反,你可以完全摆脱css动画,用普通的javascript或者它的次要的伙伴Jquery来做所有的事情。
你的选择,我的朋友,但不要像你想象的那样混合搭配它们,你不会有一段美好的时光。
https://stackoverflow.com/questions/38444170
复制相似问题