简体   繁体   English

控制CSS动画

[英]Controlling CSS animation

Is there any way to fully control the CSS animation with javascript? 有没有办法用javascript完全控制CSS动画? I need to change state of the animation by clicking the button. 我需要通过单击按钮来更改动画的状态。

@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);}}

Is there any possibility to change animation state to 50% when the button is clicked? 单击按钮时是否有可能将动画状态更改为50%?

I want to make a slider with minimum js, but need to handle this small dots. 我想制作一个最小js的滑块,但需要处理这个小点。

If there is no way to do that, could you tell me how to do that with different way? 如果没有办法做到这一点,你能告诉我如何用不同的方式做到这一点吗?

 #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> 

I'm going to go out on a limb here and say "no", even though it might be technically feasible in some bizarro way. 我打算在这里走出去并说“不”,尽管从某种奇怪的方面来说技术上可行。 Also, from a uber-nerd programmer perspective, this is violating separation of concerns (some people don't care, i know i know) as well as a pretty weird thing to do. 此外,从超级计算机程序员的角度来看,这违反了关注点的分离(有些人不关心,我知道我知道)以及一个非常奇怪的事情。

That being said, my rec is do the whole thing in CSS (ie the button click adds/toggles a class, which triggers the transition or animation). 话虽这么说,我的rec是在CSS中做的全部事情(即按钮点击添加/切换一个类,触发转换或动画)。 This way, you could have a bunch of different classes ready to go to fire different animations for different states or button presses or whatevs. 通过这种方式,您可以准备好一堆不同的类来为不同的状态或按钮或按钮触发不同的动画。

Conversely, you could be completely awesome and ditch the css-animation altogether just do the whole thing in vanilla javascript or it's lesser sidekick Jquery. 相反,你可能会非常棒,并且完全放弃了css-animation,只需要在vanilla javascript中完成整个事情,或者它是较小的伙伴Jquery。

Your choice my friend, but don't mix and match them the way you have in mind, you're not going to have a great time. 你的选择我的朋友,但不要以你的方式混合和匹配它们,你不会有一个美好的时光。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM