Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery-UI draggable reset to original position

This is probably very easy to do, but I always think too complicated.

I've just set up a simple test with #draggable / #droppable with a fixed width/height + float:left.

I then want a reset button to be able to reset the #draggable to it's original state after it's been snapped to a #droppable. (bottom line)

$(document).ready(function() {

    $("#draggable").draggable
    ({  
        revert: 'invalid',
        snap: '#droppable',
        snapMode: 'corner',
        snapTolerance: '22'

    });
});

    $("#droppable").droppable
    ({
        accept: '#draggable', 
        drop: function(event, ui) 
        {
            $(this).find("#draggable").html();
        }
});

    $(".reset").click(function() {
    /* What do I put here to reset the #draggable to it's original position before the snap */
});
like image 917
user2110966 Avatar asked Mar 04 '13 02:03

user2110966


People also ask

How do I delete a draggable element?

We can disable drag and drop on HTML elements by setting the draggable attribute to false . We set draggable to false so we can't drag it. We add event listeners for the dragstart and drop events with addEventListener .

Why is draggable not working?

You have one of these problems: Your jQuery or jQuery UI Javascript path files are wrong. Your jQuery UI does not include draggable. Your jQuery or jQuery UI Javascript files are corrupted.

What is jQuery draggable?

jQueryUI provides draggable() method to make any DOM element draggable. Once the element is draggable, you can move that element by clicking on it with the mouse and dragging it anywhere within the viewport.


1 Answers

I used this on a project

$("#reset").click(function () {
    $(".draggable-item").animate({
        top: "0px",
        left: "0px"
    });
});

did the trick for me

like image 71
craig Avatar answered Sep 22 '22 08:09

craig