Re-ordering div positions with jQuery?

天大地大妈咪最大 提交于 2019-11-29 05:18:19

Something like this perhaps:

$('.move-up').click(function(e){
    var $div = $(this).closest('div');

    // Does the element have anywhere to move?
    if ($div.index() > 0){
        $div.fadeOut('slow',function(){
            $div.insertBefore($div.prev('div')).fadeIn('slow');
        });
    }
});

$('.move-down').click(function(e){
    var $div = $(this).closest('div');

    // Does the element have anywhere to move?
    if ($div.index() <= ($div.siblings('div').length - 1)){
        $div.fadeOut('slow',function(){
            $div.insertAfter($div.next('div')).fadeIn('slow');
        });
    }
});

Demo

Basically:

  1. Grab the element you want to move ($div)
  2. Fade it out (give a nice UI effect with fadeOut())
  3. Move it either before or after the previous/next item (with insertBefore() or insertAfter())
  4. re-fade it back in (another UI effect with fadeIn())
Robin Carlo Catacutan

Yes there is by changing its css styles. Modify its position http://api.jquery.com/position/ also you can try this How to position one element relative to another with jQuery?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!