开发者

How do I add transition effects to table rows with jQuery

开发者 https://www.devze.com 2023-01-23 05:12 出处:网络
The following jQuery code swaps table rows when some buttons are clicked. I would like to know how to add a transition effect so that there\'s a fade-in or fade-out transition effect when a row is mov

The following jQuery code swaps table rows when some buttons are clicked. I would like to know how to add a transition effect so that there's a fade-in or fade-out transition effect when a row is moved (instead of the change happening instantaneously). I'm not sure where or how to apply the transition!

// Move item to top or up/down by one
$(".top,.up,.down").click(function(){

    // This row
    var row = $(this).parents("tr:first");

    // When up is pressed
    if ($(this).is(".up")) { row.insertBefore(row.prev()); }

    // When top is pressed
    e开发者_StackOverflow社区lse if ($(this).is(".top")) { var firstRow = row.parent().find("tr:first").not(row); row.insertBefore(firstRow); }

    // When down is pressed
    else { row.insertAfter(row.next()); }


In jQuery you can chain actions together... look here for a tut on chaining in jQuery

$(".top,.up,.down").click(function(){

        // This row
        var row = $(this).parents("tr:first");

        // When up is pressed
        if ($(this).is(".up")) { row.fadeOut().insertBefore(row.prev()).fadeIn(); }

        // When top is pressed
        else if ($(this).is(".top")) { 
                    var firstRow = row.parent().find("tr:first").not(row); 
                    row.fadeOut().insertBefore(firstRow).fadeIn();
                }

        // When down is pressed
        else { row.fadeOut().insertAfter(row.next()).fadeIn(); }


Use .fadeOut() and .fadeIn() like so:

// Move item to top or up/down by one
$(".top,.up,.down").click(function(){

    // This row
    var row = $(this).parents("tr:first");

    // When up is pressed
    if ($(this).is(".up")) { row.fadeOut('slow'); row.insertBefore(row.prev()); row.fadeIn('slow'); }

    // When top is pressed
    else if ($(this).is(".top")) { var firstRow = row.parent().find("tr:first").not(row); row.fadeOut('slow'); row.insertBefore(firstRow); row.fadeIn('slow');  }

    // When down is pressed
    else { row.fadeOut('slow'); row.insertAfter(row.next()); row.fadeIn('slow'); }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消