0

我通过单击按钮将行添加到预先存在的表中。为了区分添加了哪些行,我为添加的行添加了背景颜色。但是,我想在一段时间后删除此背景颜色或将其淡出。

这是我现在正在做的事情:

    $("#numlist").prepend("<tr class='newrow'><td>somenum</td></tr>");

我怎样才能淡出课程newrow

这里也是一个例子:http: //jsfiddle.net/Nx2nC/

4

3 回答 3

5

我建议使用 CSS 动画:

@-mozilla-keyframes newRowAdded {
    from {
        background-color: #ffa;
    }
    to {
        background-color: #fff;
    }
}
@-ms-keyframes newRowAdded {
    from {
        background-color: #ffa;
    }
    to {
        background-color: #fff;
    }
}
@-o-keyframes newRowAdded {
    from {
        background-color: #ffa;
    }
    to {
        background-color: #fff;
    }
}
@-webkit-keyframes newRowAdded {
    from {
        background-color: #ffa;
    }
    to {
        background-color: #fff;
    }
}
@keyframes newRowAdded {
    /* 'from' is that starting position/frame */
    from {
        background-color: #ffa;
    }
    /* 'to' is the final position/frame */
    to {
        background-color: #fff;
    }
}

.newrow {
                  /* first: the animation-name,
                     second: the animation duration (s for seconds): */
    -moz-animation: newRowAdded 2s;
    -ms-animation: newRowAdded 2s;
    -o-animation: newRowAdded 2s;
    -webkit-animation: newRowAdded 2s;
    animation: newRowAdded 2s;
}

JS 小提琴演示

使用 CSS 动画不可避免的缺点是旧浏览器,特别是但不限于 Internet Explorer,将无法实现此解决方案。如果您愿意并且能够实现 jQuery UI以及jQuery 本身,请关注那些不兼容的浏览器:

$("#add").click(function (event) {
    event.preventDefault();
    $("#numlist tbody").prepend("<tr class='newrow'><td>somenum</td></tr>").find('.newrow:first').css('background-color', '#ffa').animate({
        'background-color' : '#fff'
    }, 2000);
});

JS 小提琴演示

请注意,jQuery 本身不能color为元素的or属性设置动画background-color,但是 jQuery UI 可以。虽然有一个替代的jQuery 颜色插件也添加了这个功能,它可能比添加最小化(并且只有相关模块)jQuery UI 更轻量级。

参考:

于 2013-09-03T14:39:15.773 回答
1

您可以使用 setTimeout() 删除类

    var $el = $("#numlist").prepend("<tr class='newrow'><td>somenum</td></tr>");
    setTimeout(function() { $('.newrow',$el).removeClass('newrow')}, 1000);

你可以使用 css 过渡来实现淡化效果

.newrow {
   background-color: red;
   transition: background-color 1s linear;
}
tr {
  background-color: none;
  transition: background-color 1s linear;
}

http://jsfiddle.net/Nx2nC/13/

于 2013-09-03T14:44:51.597 回答
0

尝试这个:

$("#add").click(function (e) {    
  e.preventDefault();
  var $tr = $("<tr/>",{"class":"newrow","html":"<td>somenum</td>"}).hide();

    $("#numlist").prepend($tr.show("slow"));
    setTimeout(function(){
        $tr.css("background-color","#fff")
    },1500);
});

在这里工作小提琴:http: //jsfiddle.net/Nx2nC/9/

于 2013-09-03T14:41:09.080 回答