jQueryUI可拖动集"position:relative"在我的可拖动div上 [英] jQueryUI draggable sets "position: relative" on my draggable divs
问题描述
今天早上我在使用jQuery可拖动时遇到了一个奇怪的问题,我想知道是否还有其他人遇到过这个问题.
我在大div内有很多小div.它们是绝对定位的:对于他们的类,在CSS中为"position:absolute",而实际位置本身是根据需要在JS中计算和设置的.
现在,我要添加功能以允许这些div可拖动.
但是,一旦我将其拖到一个可拖动对象上,它就会直接在元素上被赋予"position:relative",您可能会想到,这会严重破坏屏幕上的位置.
有人知道为什么它会改变这样的位置"或如何告诉它不改变吗?
刚刚意识到有一个相当明显的答案盯着我-!对我的位置很重要:绝对!这似乎可以解决它.但是我仍然很感兴趣是否有人知道为什么这样设置位置:相对"(并且既不使其可配置,也不首先检查它是否需要位置)...我想知道我刚刚存储了哪些问题为我自己;-)
今天我遇到了相同的问题.原因是我在动态创建的元素上应用了draggable()
.我后来将其附加到dom
.当您应用draggable()
时,该元素应位于dom
中(如果样式是由类应用的.)简而言之,当它找不到与element
相连的位置时,便会添加relative
." -贾斯旺(Jashwant)
操作:.append(jElement)
然后:jElement.draggable()
出于某种原因,贾什万特(Jashwant)在问题的评论中加上了他的答案.因此,我认为将其他文章重新发布到此处会很方便.
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 ;-)
"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.
这篇关于jQueryUI可拖动集"position:relative"在我的可拖动div上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!