简体   繁体   English

如何在悬停时顺利完成活动的CSS动画?

[英]How do I smoothly complete an active css animation on hover?

So I've got this planet orbit code and what I'm trying to do on hover is to speed up the animation, have it complete one final animation cycle (at this new speed), and then stop. 因此,我有了这个行星轨道代码,我在悬停时要做的是加快动画的速度,使其完成一个最终的动画周期(以这种新速度),然后停止。 I figured how to get it to speed up on hover (apply a new animation on a parent class) but I cannot figure out how to get it to stop after one cycle has completed and return to its starting point. 我想出了如何使其在悬停时加快的速度(在父类上应用新的动画),但是我无法弄清楚如何一个周期结束使其停止并返回其起点。 play-state doesn't work, because it's too abrupt, and I can't seem to just set a new iteration-count. 播放状态不起作用,因为它太突然了,我似乎不能只设置一个新的迭代计数。 I've tried using JavaScript but I'm having some trouble figuring it out so I'd love some help. 我尝试使用JavaScript,但是在弄清楚JavaScript时遇到了一些麻烦,所以我希望获得一些帮助。

 .wrapper{ position:absolute; top:40%; left:50%; margin-left:-125px; margin-top:-65px; height:250px; width: 250px; animation:orbit 2s linear; animation-iteration-count: infinite; animation-play-state: paused; } #orbit { height:250px; width: 250px; z-index:1; border: 1px solid #989898; border-radius: 225px; animation:orbit 14s linear infinite; transform-origin:center center; } #planet { position:absolute; top:5%; left:5%; height: 50px; width: 50px; z-index:2; transform-origin:center center; background-color: orange; border-radius: 100%; } .wrapper:hover{ animation-play-state: running; } @keyframes orbit { 100% { transform:rotate(360deg); } } 
 <div class="wrapper"> <div id="orbit"> <div id="planet"> </div> </div> </div> 

If you just overwrite the animation-iteration-count in your :hover class, you'll get one orbit at the new speed. 如果您只覆盖:hover类中的animation-iteration-count ,则将以新速度获得一个轨道。 Then, you'll need to set up an event handler for when the mouse leaves the wrapper that stops everything. 然后,您需要为鼠标离开wrapper停止一切的事件设置事件处理程序。

 document.querySelector(".wrapper").addEventListener("mouseout", function(){ // Change the wrapper to only animate one more time this.style.animationIterationCount = "1"; // Remove the anmiation on the orbit document.getElementById("orbit").style.animation = "none"; }); 
 .wrapper{ position:absolute; top:40%; left:50%; margin-left:-125px; margin-top:-65px; height:250px; width: 250px; animation:orbit 2s linear; animation-iteration-count: infinite; animation-play-state: paused; } #orbit { height:250px; width: 250px; z-index:1; border: 1px solid #989898; border-radius: 225px; animation:orbit 14s linear infinite; transform-origin:center center; } #planet { position:absolute; top:5%; left:5%; height: 50px; width: 50px; z-index:2; transform-origin:center center; background-color: orange; border-radius: 100%; } .wrapper:hover{ animation-play-state: running; animation-iteration-count: 1; /* Just overwrite the iteration count */ } @keyframes orbit { 100% { transform:rotate(360deg); } } 
 <div class="wrapper"> <div id="orbit"> <div id="planet"> </div> </div> </div> 

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

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