我试图在下拉菜单的悬停效果期间实现时间延迟。我试图使用 setTimeout 和下拉菜单在悬停时正常工作,但我没有得到延迟。它只是瞬间发生。下面是我的代码:
$(document).ready(function(){
$("li.dropdown-hover").hover(hoverIn, hoverOut);
});
function hoverIn(){
setTimeout(func1(this), 3000);
}
function hoverOut(){
setTimeout(func2(this), 1000);
}
function func1(element) {
$(element).find(".dropdown-menu-hover").css("display", "block");
}
function func2(element) {
$(element).find(".dropdown-menu-hover").css("display", "none");
}
我对 jQuery 还很陌生,出于某种原因,我不想使用任何外部插件,例如 http://cherne.net/brian/resources/jquery.hoverIntent.html在许多类似的问题中都建议了这一点。有人能告诉我这里出了什么问题吗?
最佳答案
当您编写 setTimeout(func1(this), 3000);
时,您将立即调用 func1
函数。与此相同:
func1(this);
setTimeout(undefined, 3000);
相反,您希望传递 setTimeout
一个新函数,该函数将在分配的时间后运行。
var self = this;
setTimeout(function() {
func1(self);
}, 3000);
或者您可以将 this
作为额外参数传递,如 Richard B 所示。
关于jquery - 使用 jQuery 实现悬停延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35951532/