[英]Wait for a function to finish before starting next one
Probably a very simple question, but I'm a JavaScript newbie:可能是一个非常简单的问题,但我是 JavaScript 新手:
How do I wait for the "mouseenter" animation to be finished before starting the "mouseleave" animation?如何在开始“mouseleave”animation 之前等待“mouseenter”animation 完成?
Sometimes the mouse movement of the user is very quick but I want that animation to play completely mouseleave action is called.有时用户的鼠标移动非常快,但我希望 animation 完全播放 mouseleave 动作被调用。
$('.footer-links').on('mouseenter', function() {
$(this).addClass('hover');
});
$('.footer-links').on('mouseleave', function() {
$(this).removeClass('hover');
});
If you use CSS transition
or animation
use the respective "transitionend"
or "animationend"
events:如果您使用 CSS transition
或animation
使用相应的"transitionend"
或"animationend"
事件:
To keep the hover state whilst hovered, move the "transitionend"
callback inside the "mouseleave"
Event and make sure to use in CSS both the :hover
and the class .hover
together!要在悬停时保持 hover state,请在"mouseleave"
事件中移动"transitionend"
回调,并确保在 CSS 中同时使用:hover
和class .hover
!
$('.footer-links').on({ mouseenter() { $(this).addClass('hover'); }, mouseleave() { $(this).on("transitionend", function() { $(this).removeClass('hover'); }); } });
.footer-links { transition: background 2s ease-out; }.footer-links:hover, .footer-links.hover { background: red; }
<div class="footer-links">TEST ONE</div> <div class="footer-links">TEST TWO</div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
You need to bind kind of events for checking css animation.您需要绑定各种事件以检查 css animation。
el.addEventListener("animationstart", function() {}, false);
el.addEventListener("animationend", function() {}, false);
el.addEventListener("animationiteration", function() {}, false);
You can add appropriate profixed-events as well like webkitAnimationEnd
您可以添加适当的固定事件以及webkitAnimationEnd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.