gpt4 book ai didi

javascript - 在开始下一个功能之前等待一个功能完成

转载 作者:行者123 更新时间:2023-12-05 08:10:56 27 4
gpt4 key购买 nike

可能是一个非常简单的问题,但我是一个 JavaScript 新手:

如何在开始“mouseleave”动画之前等待“mouseenter”动画完成?

有时用户的鼠标移动非常快,但我希望动画完全播放,调用 mouseleave Action 。


$('.footer-links').on('mouseenter', function() {
$(this).addClass('hover');
});

$('.footer-links').on('mouseleave', function() {
$(this).removeClass('hover');
});

最佳答案

如果您使用 CSS transitionanimation,请使用相应的 "transitionend""animationend" 事件:

要在悬停时保持悬停状态,请将 "transitionend" 回调移动到 "mouseleave" 事件中,并确保在 CSS 中同时使用 : hoverclass .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>

关于javascript - 在开始下一个功能之前等待一个功能完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68886370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com