Javascript .mouseleave事件中的.delay函数
因此,我试图给鼠标离开事件添加一个延迟,这样当鼠标悬停在元素上时,它就不会出现故障Javascript .mouseleave事件中的.delay函数,javascript,jquery,menu,hover,Javascript,Jquery,Menu,Hover,因此,我试图给鼠标离开事件添加一个延迟,这样当鼠标悬停在元素上时,它就不会出现故障 $(window).load(function(){ $("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").mouseenter(function () { $("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").animate({ bottom: 75 }); }); $("#c
$(window).load(function(){
$("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").mouseenter(function () {
$("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").animate({
bottom: 75
});
});
$("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").mouseleave(function () {
.delay(10)//Have a delay here
$("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").animate({
bottom: -75
});
});
});
任何想法???在mouseleave事件中,您可以使用setTimeout延迟函数的执行。捕获setTimeout函数返回的id可以防止该函数使用clearTimeout执行。因此,如果用户在延迟完成之前将鼠标放回该区域,该元素将不会执行mouseleave动画
$(document).ready(function(){
var timeoutID ;
$("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").mouseenter(function () {
// Don't execute the hide function if it hasn't executed
clearTimeout( timeoutID );
$("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").animate({
bottom: 75
});
});
$("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").mouseleave(function () {
timeoutID = setTimeout(function(){
$("#cp_widget_7c184d64-36ed-4bb9-b617-c9034c2824c6").animate({
bottom: -75
});
}, 1000) // Delay 1000 milliseconds
});
});
这是一把小提琴:
setTimeout和clearTimeout函数的文档:
我使用这个插件,在避免“意外悬停”方面做得很好