2011-04-01 84 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