views:

2137

answers:

5

i want the draggable object to revert back to its original position when i click a button. i used 'destroy' option but it doesnt seem to work. it disables the dragging but doesnt revert back to original position.

can someone help?

EDIT PART:

$('#Zoom').toggle(function() {
          $("#draggable").draggable({});},
         function() {
       $("#draggable").draggable('destroy');});

this is a toggle button that i am using to enable dragging. the destroy option only disables dragging and does not revert the object back to the original position.

also this is the button i want to make the object come back to its original position:

nextImg.addEventListener('click', function() {img.setAttribute("src", "img"+(++imgnum)+".jpg");}, false);
A: 

Would revert help you out?

http://docs.jquery.com/UI/Draggable#option-revert

Fiona Holder
Revert returns the element to its original position when dragging stops, not when the user clicks a button.
Matthew Jones
+1  A: 

EDIT: I'm pretty sure this will do the trick:

//reposition the draggable after it has finished
$(".selector").draggable({  
    stop: function(e,ui) { 
        ui.instance.element.css("left","0px"); 
        ui.instance.element.css("top","0px"); 
    } 
});
karim79
wouldnt revert immediately revert the dragged object to its original position? i want it to happen when i click a button.
amit
@amit - see my edit
karim79
yes i tried just that. it disables the dragging but does not bring the object back to its original position. what i want to achieve is: in an image slideshow, when usr is done dragging an image, when the user clicks on next image button, the next image should display at the original position.
amit
@amit - see my edit again please. Also, feel free to buy me a beer if it works.
karim79
i am going to testdrive it right now. but wouldnt it reset the object to 0,0 of the document? i hope it resets the object to the 0,0 of the container. i owe u more than a beer. u hv been a lifesaver since yesterday.
amit
i am sorry but it doesnt work. nothing happens at all.
amit
karim79
@karim: pls see my edit in the original.
amit
+3  A: 

Revert executes when dragging has stopped (with optional delay), not on click.

$(document).ready(function() {
   var originalTop = $('#draggable').position().top;
   var originalLeft = $('#draggable').position().left;

   $('#Zoom').toggle(
         function() {
            $("#draggable").draggable({});},
         function() {
            $("#draggable").draggable('destroy');
            $('#draggable').position().top = originalTop;
            $('#draggable').position().left= originalLeft;
         });
});
KClough
This could be made more concise by just caching the original position object, but I figured this illustrated the point more verbosely
KClough
i think this might work. one problem i am having is that thingtodrag and #thingtoclick have different eventlistner functions. #thinktoclick doesnt recognize the originalTop and originalLeft variables. forgive me im fairly new to this.
amit
pls see my Edit in the original post.
amit
Edited to reflect your code, I believe this is what you are looking for
KClough
A: 

Revert option is set to false by default, you've to set it to true.

$("#draggable").draggable({ 
    revert: true
});

this should do it actually...

Sinan Y.
i know that. i explicitly remember to have set it to true.
amit
+4  A: 

How about this?

<script type="text/javascript">
jQuery(document).ready(function() { 
    $("#draggable").data("Left", $("#draggable").position().left)
              .data("Top", $("#draggable").position().top);
    $("#draggable").draggable();

    $("#nextImg").bind('click', function() {
     $("#draggable").animate(
      { "left": $("#draggable").data("Left"), 
       "top": $("#draggable").data("Top")
      }, "slow");
       $("#draggable").attr("src", "img"+(++imgnum)+".jpg");
    });
});
</script>

I've made some assumptions, such as the nextImg object having an id of "nextImg" so that I can bind the click event using jQuery (one less step in the javascript event binding process?) I'm also assuming that you don't really want to destroy the drag functionality.

Ben Koehler
i do want to destroy the draggable property. thats fine i am going to test this right now. lets see how it works out. thanks a lot for the effort! A big thumbs up!
amit
SUPER. one part of the script is working. the image resets back to the original position. but it does not switch to the next image. let me look into it. thanks a lot.
amit
GOT IT WORKING. thanks a LOT all you guys.
amit
I like the use of data, I don't use that feature enough. Good work.
KClough