jQuery draggable + droppable: how to snap dropped element to dropped-on element

前端 未结 7 1281
无人及你
无人及你 2020-11-30 19:42

I have my screen divided into two DIVs. In the left DIV I have a few 50x50 pixel DIVs, in the right DIV I have an empty g

7条回答
  •  渐次进展
    2020-11-30 20:12

    based on Barry's code, what if we d like to add an option with an "x" button the element to be detached again from the new parent and be reattached to the initial?

    i thought sth like this, but didn't seem to work.. to make some sort of a variable that hold initial state

    var flag;
    $('.draggable-div').draggable({
        revert: 'invalid',
        stop: function(){
            $(this).draggable('option','revert','invalid');
            $(this).find('.undo').show();
        flag=$(this).parent();
        }
    });
    
    
    $('.draggable-div').find('.undo').click(function(i, e) {
        var $div = $(this).parent();
    $($div).detach().appendTo(flag);
     }
    

    sth is definately wrong but i don't know if you can get the concept... just being able to reverse whatever you have dropped to their initial state.

提交回复
热议问题