Ricardo Binns
Ricardo Binns

Reputation: 3246

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?

Upvotes: 2

Views: 2794

Answers (3)

Geo P
Geo P

Reputation: 740

First reset the left position to auto, as there is still a left position from the draggable method, then set the right position.

Upvotes: 0

Ruan Mendes
Ruan Mendes

Reputation: 92334

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
    });
});

Upvotes: 4

jeschafe
jeschafe

Reputation: 2683

Because there's still a "left" value on your draggable element which takes precedence over the "right" value. You need to "unset" the left value

Upvotes: 1

Related Questions