jQuery sortable obtain 2 elements being swapped

后端 未结 5 1808
温柔的废话
温柔的废话 2020-12-04 16:18

I cannot find out how to obtain destination element with jQuery UI sortable.

    $(\"#pages\").sortable({
        opacity: 0.6,
        update: function(even         


        
5条回答
  •  一整个雨季
    2020-12-04 17:03

    You can use draggable and droppable instead of sortable to achieve swappable effect. In practise, this will look like this:

    (function() {
        var droppableParent;
    
        $('ul .element').draggable({
            revert: 'invalid',
            revertDuration: 200,
            start: function () {
                droppableParent = $(this).parent();
    
                $(this).addClass('being-dragged');
            },
            stop: function () {
                $(this).removeClass('being-dragged');
            }
        });
    
        $('ul li').droppable({
            hoverClass: 'drop-hover',
            drop: function (event, ui) {
                var draggable = $(ui.draggable[0]),
                    draggableOffset = draggable.offset(),
                    container = $(event.target),
                    containerOffset = container.offset();
    
                $('.element', event.target).appendTo(droppableParent).css({opacity: 0}).animate({opacity: 1}, 200);
    
                draggable.appendTo(container).css({left: draggableOffset.left - containerOffset.left, top: draggableOffset.top - containerOffset.top}).animate({left: 0, top: 0}, 200);
            }
        });
    } ());
    

    Demo, http://jsfiddle.net/FZ42C/1/.

提交回复
热议问题