How do I move list items?

前端 未结 3 583
终归单人心
终归单人心 2021-02-08 03:45

I have a normal unordered list

  • Item 1
  • Item 2
  • Item 3
  • Item 4
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 04:40

    This does everything but the animation:

    $('li').click(function() {
        var $this = $(this);
        $this.insertAfter($this.siblings(':eq(0)'));
    });
    

    When you click on a list item, it will insert it after the first item in the

      , i.e. the second position in the list.

      Furthermore, you could animate this in various ways. Here's one:

      $('li').click(function() {
          var $this = $(this),
              callback = function() {
                  $this.insertAfter($this.siblings(':eq(0)'));
              };
          $this.slideUp(500, callback).slideDown(500);
      });
      

      Here's a working demo.

提交回复
热议问题