javascriptjquerypositiondraggable

Set right position in a draggable div


What I'm trying to do:

Turn a div as draggable, then make a button to reset its position as default.

The question is, why can’t I set the right position of a absolute div, after I set as draggable?

Drag the div first and then click in the button.

$('div').draggable();
$('#button').click(function(){
   $('div').draggable('destroy').css({ // Destroy was only a test
       top: 0,
       right: 0 // Nothing happens
   });
});

jsfiddle demo.

The position only works if I use left.

The reason I'm not using the left position, it’s because if I zoom in, will overlay other things. Any tips, be my guest.

What am I missing?


Solution

  • It's because $.draggable() sets left as you drag the div around, and it stays set after you call $.draggable('destroy')

    Just reset left using left: auto; http://jsfiddle.net/9uLCY/7/

    $('div').draggable();
    $('#button').click(function(){
       $('div').draggable('destroy').css({
            top: 0,
            left: 'auto',
            right: 0
        });
    });