2011-04-01 69 views
0

這是我如何使用jQuery hoverjQuery的懸停

懸停(handlerIn(eventObject)傳遞, handlerOut(eventObject)傳遞)

$(".box").each(function() { 
    var $this = $(this); 
    $this.data('baseColor', $this.css('background-color')); 
    $this.hover(function() { 
     $this.animate({ 
      backgroundColor : "white" 
     }, 50); 
    }, function() { 
     $this.animate({ 
      backgroundColor : $this.data('baseColor') 
     }, 50); 
    }); 
}); 

問題是當DOM改變懸停效果不再有效。我知道方法多次爲我解決了這樣的問題,但我該如何解決它呢?

+1

看看我的回答,我確定了問題,並且還將您鏈接到可能幫助您實現動畫的腳本。 – Khez 2011-04-09 09:06:21

回答

3

manual

所有動畫處理的屬性應該被動畫 到單個數字值,除非 下面指出的;大多數 非數字屬性不能使用 基本jQuery功能進行動畫製作。 (對於 示例,寬度,高度或左側可以爲 動畫,但背景色不能爲 )。除非指定了其他 ,否則將屬性值視爲 像素數。在適用的情況下,單位em和%可以是 。

具體For example, width, height, or left can be animated but background-color cannot be.

如果你想只是爲了更新背景顏色爲白色,請嘗試:

$this.hover(function() { 
     $this.css('background-color', '#fff'); 
}, function() { 
     $this.css('background-color',$this.data('baseColor')); 
}); 

如果你想使效果出現在用戶懸停後50毫秒,請嘗試使用delay

現在,如果您嘗試從當前顏色轉換爲白色,則需要了解顏色混合。但爲了節省您的麻煩,這裏的優秀script by Michael Leigeber。它支持背景顏色褪色。


+0

感謝您指出真正的問題。我刪除了我的答案並投票給你。 – BoltClock 2011-04-09 09:10:16

+0

有沒有必要刪除答案...雖然對於投票Ty ... – Khez 2011-04-09 09:11:13

+0

+1 @Khez你完全正確的當然!我忘了這一點,並假定OP有工作代碼,停止DOM更新工作。接得好 – JohnP 2011-04-09 09:40:42