2012-03-09 56 views
0

下面我寫一個函數的懸停元素。我想僅當鼠標上的2個或更多秒的部分執行此功能。當他們會少或根本不是這樣做的。執行的功能在2秒後

下面的代碼中,我寫道:

$(function(){ 
    $('section.zespol_list ul li a').hover(function(){ 
     $(this).next().fadeIn(1000); 
    }, 
    function(){ 
     $(this).next().fadeOut(1000); 
    }); 
}); 

回答

6

您可以使用setTimeout延遲執行和在這段時間內,如果鼠標移開觸發明確使用clearTimeout計時器。

$(function(){ 
    var timeOutId; 
    $('section.zespol_list ul li a').hover(function(){ 
     timeOutId= setTimeout(function(){ 
      $(this).next().fadeIn(1000) 
     ), 2000); 
    }, 
    function(){ 
     clearTimeout(timeOutId); 
     $(this).next().fadeOut(1000); 
    }); 
}); 
1

使用setTimeout()clearTimeout()

var timer; 
$(function(){ 
    $('section.zespol_list ul li a').hover(function(){ 
     timer = setTimeout(function() { 
      $(this).next().fadeIn(1000); 
     }, 2000); 
    }, 
    function(){ 
     clearTimeout(timer); 
     $(this).next().fadeOut(1000); 
    }); 
}); 
+0

只是想解決它,但它並沒有在所有的工作。下面我的代碼連接到網絡版: http://tinyurl.com/7j9934v 這個測試可以排列成正方形板的照片。 – Pietrov 2012-03-09 19:59:46

+0

@Pietrov這不是我的錯,有一個錯誤:'未捕獲TypeError:對象[對象對象]沒有方法'slideViewerPro'' – 2012-03-09 20:01:08

+0

好吧我刪除了問題,並沒有在代碼上工作。其他腳本會影響他的工作嗎? – Pietrov 2012-03-09 20:08:53

0

清楚它其實有一個插件叫做HoverIntent。我沒有用它個人,但似乎將解決您的問題

+0

這不會幫助,因爲它不會被取消 – 2012-03-09 19:47:40