Jquery UI 可拖动绝对而不是相对?
我有一个包含一些jquery可拖动对象的div,但是如果在任何时候我从页面中删除其中一个可拖动对象,那么这可以移动其余的它们,因为它们是相对定位的。
这是一个展示这一点的小提琴: http://jsfiddle.net/VolatileStorm/aNk6e/
我建议的解决方案是可拖动应该使用绝对定位而不是相对定位,但是我找不到办法做到这一点。有没有办法做到这一点,如果没有,有人能想出解决办法吗? (为了优雅起见,我不会接受“不要删除其他可拖动的”)。
I have a div containing a few jquery draggables, however if at any point I remove one of these draggables from the page then this can move the rest of them as they are positioned with relative.
Here is a fiddle showcasing this:
http://jsfiddle.net/VolatileStorm/aNk6e/
My proposed solution is that the draggable should be using absolute as opposed to relative positioning, however I can't find a way to do this. Is there a way to do this, and if not, can anyone think of a way around it? (For elegance's sake, I won't accept "don't delete the other draggable").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
position:absolute
添加到您的元素中。.draggable
之前将它们附加到 DOM。position:absolute
to your elements..draggable
.为什么
position:absolute
不适合您?由于绝对定位的元素不再是文档流的一部分,因此三个
中的每一个都放置在其包含(定位)元素的
0,0
处。因此,您需要计算出可拖动对象的起始位置。Why doesn't
position:absolute
work for you?Since absolutely positioned elements are no longer part of the document flow, each of your three
<div>
s are placed at0,0
of its containing (positioned) element. Thus, it's up to you to work out the starting position of your draggables.将元素设置为可拖动时,可以使用 jQuery css 函数将位置设置为绝对位置。
You can set the position to be absolute by using the jQuery css function when setting an element to be draggable.
也许不是最好的想法,但是...您可以使用 positon:relative; 定位所有相关的 div。然后在你的或 $("document").ready() 中包含一个函数调用,如果你使用的是 jQuery,它会迭代每个 div 将它们的相对位置转换为绝对位置并将它们设置为它们的左、上之前将它们更改为位置:绝对。这里唯一的问题是,您希望每个 div 在更新其样式之前具有绝对位置,以免通过迭代改变布局。
Perhaps not the best ideas but... You could position all the relevant divs using positon:relative; and then include a function call in your or $("document").ready(), if you're using jQuery, whic iterates through each of the divs to translate their relative positions into absolute and set them as their left, top before changing them to position:absolute. Only problem here is that you'd want each divs absolute position prior to updating their styles so as not to alter the layout through iteration.