Javascript Callback.hover函数不工作
我有一个js函数:Javascript Callback.hover函数不工作,javascript,jquery,callback,Javascript,Jquery,Callback,我有一个js函数: $('#linkNext').on({ mouseenter: function () { if ($('#conteudo .boxes').hasClass('ativo')) { $('#conteudo .boxes').removeClass('ativo'); $('.boxAberto').animate({width: '0'}, 600, function () {
$('#linkNext').on({
mouseenter: function () {
if ($('#conteudo .boxes').hasClass('ativo')) {
$('#conteudo .boxes').removeClass('ativo');
$('.boxAberto').animate({width: '0'}, 600, function () {
// callback
$('#linkNext').hover(function () {
this.iid = setInterval(function () {
if (cont > -565) {
cont -= 5;
$('#conteudo').attr('style', 'left:' + cont + 'px');
console.log(cont)
}
if (cont <= -565) {
$('#linkNext').hide();
}
}, 0);
});
$('#linkNext').mouseleave(function () {
this.iid && clearInterval(this.iid);
});
// callback ends
});
} else {
this.iid = setInterval(function () {
if (cont > -565) {
cont -= 5;
$('#conteudo').attr('style', 'left:' + cont + 'px');
console.log(cont)
}
if (cont <= -565) {
$('#linkNext').hide();
}
}, 0);
}
},
mouseleave: function () {
this.iid && clearInterval(this.iid);
}
});
替换以下代码:
if ($('#conteudo .boxes:not(.ativo)')) {
$('#linkNext').hover(function () {
this.iid = setInterval(function () {
对于这一个:
if ($('#conteudo .boxes:not(.ativo)').length) {
var dees = this;
$('#linkNext').hover(function () {
dees.iid = setInterval(function () {
...
...
, function () {
dees.iid && clearInterval(dees.iid);
});
因为在执行这个.iid
时,您正在为回调函数原型指定一个iid
属性,在这里,您实际上希望将它添加到.box
jquery元素中,就像您的代码所暗示的那样
因此,在进入回调之前,您必须将this
捕获到一个变量,我使用var dees=this代码>
另外,当您想检查jquery元素的存在性时,请使用.length
属性。您使用的是什么版本的jquery?为什么要在mouse enter函数中设置悬停函数和mouseleave函数?因为我是一名初学者。我不知道这是否是最好的方法,但它是有效的,唯一的回调我不知道为什么不起作用..你在每一个鼠标指针上重新绑定hover()函数,它不会在mouseleave上消失,你只是继续堆积?但是如果我不使用mouseleave,我应该用什么呢?它起作用了,但是我必须离开#linkNext
的鼠标,再次将鼠标移到setInterval
上。。如何修复?你调用setInterval时使用了一个零参数作为interval duration,为什么?事实上,我需要知道鼠标是否仍然悬停,或者我尝试过的.is(“:hover”)
,但它似乎不起作用..在鼠标按下时添加一个类或属性,删除mouseleave上的属性。通过这种方式,您可以随时检查该属性或类,以了解鼠标是否在其上。我将其设置为毫秒setTimeout
,对吗?我选择0使其在鼠标悬停链接时不停止循环
if ($('#conteudo .boxes:not(.ativo)').length) {
var dees = this;
$('#linkNext').hover(function () {
dees.iid = setInterval(function () {
...
...
, function () {
dees.iid && clearInterval(dees.iid);
});