Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQueryUI draggable sets "position: relative" on my draggable divs

I'm having an odd problem with jQuery draggable this morning, I wonder if anyone else has come across this.

I have many small divs inside a large div. They are absolutely positioned: "position:absolute" in CSS for their class, with the actual position itself calculated and set in JS on demand.

Now I'm adding functionality to allow these divs to be draggable.

But, as soon as I make one draggable, it is given "position:relative" directly on the element, which as you might imagine, seriously messes up the on screen position.

Does anyone know why it changes the "position" like this or how to tell it not to?

EDIT:

Just realised there is a rather obvious answer staring me in the face - !important on my position:absolute! This seems to fix it. BUT I'm still interested if anyone knows why it sets "position: relative" like this (and doesn't either make it configurable or check first if it needs position)...I'm wondering what problems I've just stored up for myself ;-)

like image 982
Oli Comber Avatar asked Aug 30 '12 10:08

Oli Comber


2 Answers

"I came across the same problem today. The reason was I was applying draggable() on a dynamically created element. I was 'later' appending it to dom. The element should be in dom when you apply draggable() (if style is being applied by a class). In short, when it finds no position attached with the element , it adds relative." - Jashwant

Firs do: .append(jElement) Then: jElement.draggable()

For some reason Jashwant put his answer in the comment to the question. So I thought it will be convenient to other to repost it here.

like image 61
Dmitry Akhonin Avatar answered Nov 05 '22 10:11

Dmitry Akhonin


It also happened to me, but only on Chrome. Reason?

It was like this:

$("#div-popup").draggable({ handle: ".top", containment: 'document'});

Then I removed the containment parameter, like this:

$("#div-popup").draggable({ handle: ".top"});

So it's about the Browser (Chrome in this case), which sets position to Relative when you specify which containment the element will be draggable.

like image 1
felippe Avatar answered Nov 05 '22 10:11

felippe