我正在嵌套菜单上工作,当鼠标移过一个选项时,会出现一个子列表。 这里是我的悬停功能:如何在悬停功能执行前添加一个等待时间
$(".sublist").parent().hover(function() {
$(this).toggleClass("li_hover",300); //use to change the background color
$(this).find(".sublist").toggle("slide", {}, 500); //sub list show/hide
});
现在,我想添加一个短周期前的子列表显示出来,以防止疯狂老鼠从用户移动。有人对此有很好的建议吗?
更新:
感谢你们,我做了我的程序一点点的变化,最近,它看起来是这样的:
function doSomething_hover (ele) {
ele.toggleClass("li_hover",300);
ele.find(".sublist").toggle("slide", {}, 500);
}
$(function() {
$(".sublist").parent().hover(function() {
setTimeout(doSomething_hover($(this)), 3000);
});
}):
这是奇怪的是setTimeout的不会耽误什么。但如果我将函数调用更改为doSomething_hover
(不带“()”),该函数将延迟良好。但我不能通过任何jQuery元素的功能,所以它仍然不起作用,有人可以告诉我,如何使doSomething_hover($(this))
工作在setTimeout
?
更新2: 得到了setTimeout的工作,但似乎不是我想要的: 我到底要什么会发生,如果小于0.5秒一个选项鼠标悬停。
总之,这里是我的代码做的setTimeout工作:
function doSomething_hover (ele) {
ele.toggleClass("li_hover",300);
ele.find(".sublist").toggle("slide", {}, 500);
}
$(function() {
$(".sublist").parent().hover(function() {
var e = $(this);
setTimeout(function() { doSomething_hover(e); }, 1000);
});
}):
最后更新: 我用clearTimeout当我出去移动鼠标得到了这个工作。 所以代码应该是:
$(".sublist").parent().mouseover(function() {
var e = $(this);
this.timer = setTimeout(function() { doSomething_hover(e); }, 500);
});
$(".sublist").parent().mouseout (function() {
if(this.timer){
clearTimeout(this.timer);
}
if($(this).hasClass("li_hover")){
$(this).toggleClass("li_hover");
}
$(this).find(".sublist").hide("slide", {}, 500);
});
这是$(document).ready()
的部分。其他代码将与上面相同。
真。最终更新: 因此,mouseover
和mouseout
有时会导致错误,因为当我将鼠标移动到子列表时,父母的mouseover
事件将触发,并隐藏子列表。 问题可以通过使用hover
功能来解决:
$(".sublist").parent().hover(
function() {
var e = $(this);
this.timer = setTimeout(function() { doSomething_hover(e); }, 500);
},
function() {
if(this.timer){
clearTimeout(this.timer);
}
$(this).find(".sublist").hide("slide", {}, 500);
if($(this).hasClass("li_hover")){
$(this).toggleClass("li_hover",300);
}
}
);
感谢所有
感谢您的答案,我已经尝试了您的解决方案,但子列表不会显示,但如果我更改doSomething_hover() (“你好”),它工作的很好。你知道这件事吗? – Anar
我认为这种逻辑将是:结合所述函数应用于所述悬停事件1秒后? – Anar
我明白了,使用clearTimeout,谢谢哥们! – Anar