javascript - 防止mouseleave函数中断mouseenter
问题描述
我使用 jQuery mouseenter/mousleave 来执行带有我的徽标的功能。我也使用 Animate.css。
$(".logo-1").mouseenter(function() {
$(this).css("display", "none");
$(".logo-2").css("display", "inline-block");
$("#facebook").css("visibility", "visible").css("-webkit-animation-duration", "0.1s").addClass("animated fadeInDown").one("animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd",
function() {
$("#linked").css("visibility", "visible").css("-webkit-animation-duration", "0.1s").addClass("animated fadeInDown").one("animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd",
function() {
$("#instagram").css("visibility", "visible").css("-webkit-animation-duration", "0.1s").addClass("animated fadeInDown").one("animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd",
function() {
$("#github").css("visibility", "visible").css("-webkit-animation-duration", "0.1s").addClass("animated fadeInDown").one("animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd",
function() {
$("#facebook, #linked, #instagram, #github").removeClass("animated fadeInDown")
});
});
});
});
});
$(".logo-social").mouseleave(function() {
$("#github").addClass("animated fadeOutUp").one("animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd",
function() {
$("#instagram").addClass("animated fadeOutUp").one("animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd",
function() {
$("#linked").addClass("animated fadeOutUp").one("animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd",
function() {
$("#facebook").addClass("animated fadeOutUp").one("animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd",
function() {
$("#facebook, #linked, #instagram, #github").removeClass("animated fadeOutUp").css("visibility", "hidden");
$(".logo-2").css("display", "none");
$(".logo-1").css("display", "inline-block");
});
});
});
});
});
HTML:
<div class="logo-social">
<div class="dg">
<img src="img/logo-1.png" class="logo-1" alt="logo">
<img src="img/logo-2.png" class="logo-2" alt="logo">
</div>
<div class="wrapper">
<div class="media-links-1">
<a href="https://www.facebook.com/gorniczy" target="_blank"><img src="img/facebook-logo-dark.png" id="facebook" alt="Facebook logo"></a>
<a href="https://www.linkedin.com/in/denis-gornichar-56b3b564/" target="_blank"><img src="img/linkedin-sign-dark.png" id="linked" alt="LinkedIn logo"></a>
<a href="https://www.instagram.com/gorniczy/" target="_blank"><img src="img/instagram-symbol-dark.png" id="instagram" alt="Instagram logo"></a>
<a href="https://github.com/gorniczy" target="_blank"><img src="img/github-sign-dark.png" id="github" alt="Github logo"></a>
</div>
</div>
</div>
当我等到 mouseenter 函数完成后再将光标移动到其他地方时,一切都很好。但是如果我在第一个函数完成之前移动光标,它会被第二个函数打断,从而导致错误。
我希望我的 mouseleave 函数只有在 mouseenter 函数完成后才开始执行,无论我何时将光标移动到其他地方。
解决方案
您的动画有 4~5 个回调堆栈。
所以我理解你为什么要研究“触发预防”。
这是一种使用“标志”的方法。每个“禁用”状态一个......如果鼠标进入或退出徽标,则一个总是现在。
然后,使用超时和那些标志,您可以阻止动画执行......同时确保在“in”结束后鼠标不在时执行“out”动画。
var disableIn = false,
disableOut = false,
animationDelayIn = 600, // Set those two delays based on your animation times
animationDelayOutIn = 600,
currentlyIn = false;
$(".logo").mouseenter(function(){
currentlyIn = true;
if(!disableIn){
//otherAnimation
}
// Toggle the flag during the animation
disableIn = true;
setTimeout(function(){
disableIn = false;
},animationDelayIn);
});
$(".logo").mouseleave(function(){
currentlyIn = false;
if(!disableOut){
//otherAnimation
}
// Toggle the flag during the animation
disableOut = true;
setTimeout(function(){
disableOut = false;
if(!currentlyIn){
$(".logo").trigger("mouseleave"); // If the mouse is out, yeah, do the out animation now.
}
},animationDelayOut);
});
免责声明:这是一个很好的开始......它需要用你的动画进行测试。;)
推荐阅读
- pandas - 一些 Pandas 子图为空白或未正确显示
- python - 将密钥发送到元素 selenium python
- flutter - setState 无助于重建小部件树
- python-3.x - 单击 - TypeError:尝试将回调转换为命令两次
- excel - 无法在没有 VBA 显示错误 438 的情况下写入 vode
- java - 消息消费时避免无限循环
- kotlin - 从自定义类注解中获取类属性
- next.js - 如何在 Nextjs 中使用带有预加载的谷歌字体
- node.js - 用户登录时如何从看不见的帖子中更新用户的提要?
- python - Python-Tkinter 试图删除标签