2013-04-26 14 views
0

當我將鼠標移過.test我希望.hide顯示,以及何時將鼠標移出。測試我希望.hide隱藏我的jquery函數執行了很多時間而不是一個

它不幸顯示它隱藏不止一次。

我的代碼是:

.hide{ 
    opacity: 0; 
    filter: "old-ie-staff"; 
} 

$(document).on('mouseover', '.test', function() { 
    $(this).find('.hide').animate({opacity: 1},300); 
}).on('mouseout', function() { 
    $(this).find('.hide').animate({opacity: 0},300); 
}); 

這裏小提琴鏈接:http://jsfiddle.net/malamine_kebe/2bnZW/

+3

你應該看看在'.animate()'調用之前添加['.stop(true)'](http://api.jquery.com/stop/)。 – andyb 2013-04-26 08:48:36

+0

非常感謝andyb – 2013-04-26 08:53:46

回答

2

動畫序列將元素執行mouseovermouseout時進行排隊。 jQuery提供了一種停止這些動畫並將其從動畫隊列中移除的方法。

添加.stop(true)之前.animate()

停止對匹配的元素

true參數也將刪除所有已經排隊動畫當前正在運行的動畫 - 見Prevent Animation Queue Buildup

1

你只需要使用stop()清除排隊動畫;

$(document).on('mouseover', '.test', function() { 
    $(this).find('.hide').stop(true).animate({opacity: 1},300); 
}).on('mouseout', function() { 
    $(this).find('.hide').stop(true).animate({opacity: 0},300); 
}) 
相關問題