2013-04-08 83 views
0

我有這個js函數:回調.hover功能心不是工作

$('#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); 
    } 
}); 

鼠標懸停時,它會檢查元素具有類.ativo。如果有,它將刪除該類併爲元素設置動畫,並調用回調。回調沒有工作,我想即時用錯誤的方式使用.hover

else它將開始setInterval這是工作很好,mouseleave功能也是如此。我的問題是唯一的回調,我不知道我在做什麼錯

* 編輯 **

現在我改變了代碼,只使用.hover有回調,而不是使用.onmouseenter mouseleave。但我仍然有同樣的問題。行// callback心不是工作上面的回調..

$('#linkNext').hover(function() { 
    if ($('#conteudo .boxes').hasClass('ativo')) { 
     $('#conteudo .boxes').removeClass('ativo'); 
     $('.boxAberto').animate({width: '0'}, 600, function() { 
      // callback 
      if ($('#conteudo .boxes:not(.ativo)')) { 
       $('#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); 
       }, function() { 
        this.iid && clearInterval(this.iid); 
       }); 
      } 
     }); 
    } 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); 
    } 
}, function() { 
    this.iid && clearInterval(this.iid); 
}); 
+0

你用什麼版本的jQuery? – PlantTheIdea 2013-04-08 21:53:28

+0

即時通訊使用v1.8.3 – 2013-04-08 21:54:30

+0

爲什麼在鼠標輸入功能中設置懸停功能和鼠標離開功能? – Ohgodwhy 2013-04-08 21:58:24

回答

1

替換您如下代碼:

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); 
      }); 

,因爲這樣做this.iid當你asigning一個iid屬性到你的回調函數原型,看起來你真的想把它添加到你的.boxes代表你的代碼隱含的jQuery元素。

所以,你必須捕捉this一個變量之前,輸入您的回調,我var dees = this;

做此外,當您想檢查一個jQuery元素是否存在等,使用.length屬性。

+0

它的工作原理,但我必須離開'#linkNext'的鼠標,然後再次將鼠標移到'setInterval'上。我該如何解決這個問題? – 2013-04-08 23:05:22

+0

你打電話setInterval與零參數作爲間隔持續時間,爲什麼? – Nelson 2013-04-08 23:12:25

+0

事實上,我需要知道,如果鼠標仍然懸停或鏈接的東西,我已經嘗試'.is(':hover')'但它似乎並沒有工作.. – 2013-04-08 23:13:10