JQuery-UI draggable reset to original position
Asked Answered
W

6

15

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 */
});
Wilmawilmar answered 4/3, 2013 at 2:29 Comment(0)
C
13

Draggable items don't keep track of their original position that I know of; only during drag and to be snapped back. You can just do this on your own, though:

$("#draggable").data({
    'originalLeft': $("#draggable").css('left'),
    'origionalTop': $("#draggable").css('top')
});

$(".reset").click(function() {
    $("#draggable").css({
        'left': $("#draggable").data('originalLeft'),
        'top': $("#draggable").data('origionalTop')
    });
});

http://jsfiddle.net/ExplosionPIlls/wSLJC/

Cimex answered 4/3, 2013 at 2:38 Comment(8)
Seems to be what I need. However, when I add $("#draggable").data({'originalLeft': $("#draggable").css('left'), 'origionalTop': $("#draggable").css('top') }); The draggable stops workingWilmawilmar
@Wilmawilmar I think your comment is incomplete?Cimex
I edited it, sorry about that. "The draggable stops working". I even tried to just copy/paste the entire thing. That's weird, as it clearly works on the demo.Wilmawilmar
@Wilmawilmar hard to say without seeing the rest of your code / websiteCimex
jsfiddle.net/6TNeZ/3 that's basically it.. Tried looking for errors, but couldn't find any.Wilmawilmar
I see an error; you were not closing the string on '#droppable: jsfiddle.net/ExplosionPIlls/6TNeZ/4Cimex
Oh.. Now I feel stupid. Thanks a lot for your help, very much appreciated.Wilmawilmar
Worked like a charm!Microdont
S
21

I used this on a project

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

did the trick for me

Saponify answered 16/2, 2014 at 13:49 Comment(2)
I used a combination of this and getting the original coordinates from the other answer rather than setting it back to 0,0.Conventionalize
this is excellent for none absolute positions looks real nice to!Chema
C
13

Draggable items don't keep track of their original position that I know of; only during drag and to be snapped back. You can just do this on your own, though:

$("#draggable").data({
    'originalLeft': $("#draggable").css('left'),
    'origionalTop': $("#draggable").css('top')
});

$(".reset").click(function() {
    $("#draggable").css({
        'left': $("#draggable").data('originalLeft'),
        'top': $("#draggable").data('origionalTop')
    });
});

http://jsfiddle.net/ExplosionPIlls/wSLJC/

Cimex answered 4/3, 2013 at 2:38 Comment(8)
Seems to be what I need. However, when I add $("#draggable").data({'originalLeft': $("#draggable").css('left'), 'origionalTop': $("#draggable").css('top') }); The draggable stops workingWilmawilmar
@Wilmawilmar I think your comment is incomplete?Cimex
I edited it, sorry about that. "The draggable stops working". I even tried to just copy/paste the entire thing. That's weird, as it clearly works on the demo.Wilmawilmar
@Wilmawilmar hard to say without seeing the rest of your code / websiteCimex
jsfiddle.net/6TNeZ/3 that's basically it.. Tried looking for errors, but couldn't find any.Wilmawilmar
I see an error; you were not closing the string on '#droppable: jsfiddle.net/ExplosionPIlls/6TNeZ/4Cimex
Oh.. Now I feel stupid. Thanks a lot for your help, very much appreciated.Wilmawilmar
Worked like a charm!Microdont
W
8

I've just had great success with the following:

$(".draggable-item").removeAttr("style");
Womble answered 15/11, 2016 at 15:19 Comment(1)
This answer did help me, but it also removes the 'position: relative', causing the next drag action to not animate in the original way.Gurtner
C
4

I found that the following was the simplest way to reset to original position, but if you want the item to remain draggle this will not work alone

$(".draggable-item").removeAttr('style');

This worked for me to reset to original position and keep item draggable:

$("#reset").click(function () {
    // Reset position
    $(".draggable-item").removeAttr('style');

    // Destroy original draggable and create new one
    $(".draggable-item").draggable("destroy");
    $(".draggable-item").draggable();
});
Consideration answered 15/6, 2018 at 22:34 Comment(0)
D
2

I used h0dges' basic idea. The balls I created returned to their original positions, but I lost the styles that made them into balls. Therefore, I kept the styles of the balls and just reset their top and left styles to return them to their original positions:

function endRound() {
   $(".ball").css({"top":"", "left":""});
}
Dryden answered 18/11, 2016 at 12:50 Comment(0)
H
0

I don't know exactly what people wish to see but I have made a fiddle about resetting objects to initial positions on a single click either vertical or horizontal.Fiddle https://jsfiddle.net/JunaidAli/wp1n796q/321/

Honesty answered 15/7, 2020 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.