jQuery.draggable() - Revert on button click

后端 未结 1 1092
刺人心
刺人心 2021-01-01 01:49

I have several draggable & droppable elements on my page, which have accept properties.

Currently my code is set up like:

$(\".answe         


        
1条回答
  •  情深已故
    2021-01-01 02:14

    Since there's no built-in method to do what you need, you'd have to simulate the revert behavior yourself. You can store the original position of each draggable element and then when clicking a reset button, animate them back to those positions.

    Here's a simplified jsFiddle example.

    jQuery:

    $("#draggable").draggable({
        revert: "invalid"
    });
    $("#draggable2").draggable({
        revert: "invalid"
    });
    $("#droppable").droppable({
    });
    $("#btnReset").click(function() {
        $("#draggable, #draggable2").animate({
            "left": $("#draggable").data("left"),
            "top": $("#draggable").data("top")
        });
    });
    $(".ui-widget-content").data("left", $(".ui-widget-content").position().left).data("top", $(".ui-widget-content").position().top);
    

    HTML:

    I revert when I'm dropped

    I revert when I'm dropped

    Drop me here

    0 讨论(0)
提交回复
热议问题