Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery draggable options

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);
like image 327
amit Avatar asked Jun 24 '09 20:06

amit


People also ask

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.

How do I limit a draggable area?

Limit draggable area using 'containment' option It is simple. All you have to do is, add an option called containment to the draggable() method. The containment option has a value parent.

How do you use draggable and droppable?

This can be done by clicking on the draggable object by mouse and drop it on the specified target. The value of this option specifies that which draggable objects can be dropped on the specified target. The default value of this option is *.


2 Answers

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.

like image 105
Ben Koehler Avatar answered Oct 19 '22 19:10

Ben Koehler


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;
         });
});
like image 23
KClough Avatar answered Oct 19 '22 20:10

KClough