0

这就是我使用jQuery 悬停的方式

悬停(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 更改时,悬停效果不再起作用。我知道live方法为我解决了很多次这样的问题,但在这种情况下我该如何解决呢?

4

1 回答 1

3

手册

所有动画属性都应动画为单个数值,除非以下说明;大多数非数字属性无法使用基本的 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

现在,如果您尝试从当前颜色过渡到白色,则需要了解颜色混合。但是为了省去你的麻烦,这里有一个Michael Leigeber的优秀剧本。它支持背景颜色褪色。


于 2011-04-09T09:03:32.347 回答