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 */
});
ScottyG
  • 3,204
  • 3
  • 32
  • 42
user2110966
  • 155
  • 1
  • 1
  • 7

6 Answers6

21

I used this on a project

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

did the trick for me

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
craig
  • 211
  • 2
  • 2
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/

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
7

I've just had great success with the following:

$(".draggable-item").removeAttr("style");
h0dges
  • 663
  • 7
  • 15
  • This answer did help me, but it also removes the 'position: relative', causing the next drag action to not animate in the original way. – MeanGreen May 25 '20 at 07:20
3

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();
});
ScottyG
  • 3,204
  • 3
  • 32
  • 42
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":""});
}
Yoko Ishioka
  • 161
  • 5
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/

Junaid Ali
  • 361
  • 2
  • 9