Reset a div to a pre-dragged position

Hello all w/jquery-ui i can drag & drop a div.
i can then disable it w/a timer.
But what i’d like to do is after a certain amount of time have the div return to it’s original position (and animate/smooth the return back in). So far have not found the info on how to that.
I can disable it, destroy but can’t find how to return it.
Hope you folks can advise. this is my code so far.


$(document).ready(function() {
		

$( "div" ).draggable({
  zIndex: 100,
  containment: "#primary"
});
var zIndex = $( "div" ).draggable( "option", "zIndex" );
 
// Setter
$( ".div" ).draggable( "option", "zIndex", 100 );

var stack = $( ".selector" ).draggable( "option", "stack" );
	

	
		
var timer = setInterval(function () {myTimer()}, 10000);
	function myTimer() {
		$( "div" ).draggable( 'disable' )
	}
	
	
});

and they it would be nice if the user could drag it back again. but the div would always eventually reposition itself.

Hey Sherpa,

when dragging starts why not grab the current coordinates, then on dragstop set a timeout event to animate it back to its original position (using these coordinates). If the user drags it before the timeout has fired, cancel the timeout.

You can get the element’s position like this:

var position = $(element).offset();

If you don’t mind positioning the element absolutely, you can also do it like this:

<div id="draggable" class="ui-widget ui-widget-content" data-top="50px" data-left="100px">
  <p>Drag me!</p>
</div>

var $d = $("#draggable"),
    t;

$d.draggable({
    start: function() {
        clearTimeout(t);
    },
    stop: function() {
        t = setTimeout(function(){
            $d.animate({
               top: $d.data("top"),
               left: $d.data("left")
            });
        }, 1000);
    }
});

DEMO

Hello Pullo! Thank you for you help and the demo code as well. i’d love to try to use the offset. setting a an absolute pos for all the divs on the page doesn’t’ seem like a good idea.
how could i use the offest to return the moved divs back to their original spots please?
thx
D

Can you provide some simple markup to use?

sure thank you will put it on codepen later today!

sorry! not forgotten will try to do this weekend coming up.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.