如何防止可调整大小和可拖动元素相互折叠?
嘿大家。我有以下代码:
如您所见,有 2 个一层一层地堆叠的简单 DIV。每个 DIV 也都设置为可调整大小和可拖动。但是,请注意,当您尝试调整第一个元素的大小时,第二个元素会折叠到第一个元素上。据我所知,这是因为可调整大小将元素更改为绝对位置。我怎样才能防止这种行为?是否可以在保留拖动元素的能力的同时调整元素的大小?
另请注意,如果您将元素更改为具有相对位置(在 .demo 样式中添加position:relative !important),它可以防止折叠,但当您开始调整大小或拖动时,元素会“跳跃”。又一个奇怪的行为。感谢您的帮助。
Hey all. I have the following code:
As you can see, there are 2 simple DIVs that are stacked one above each other. Each of these DIVs are also set to be resizable and draggable. However, notice when you attempt to resize the first element, the second element collapses onto the first. From what I can see, this is because resizable changes the element to a position of absolute. How can I prevent this behavior? Is it possible to resize the element while retaining the ability to drag the element?
Note also, that if you change the elements to have a position of relative (add position:relative !important in the .demo style), it sort of prevents the collapsing, but the element "jumps" when you begin to resize or drag. Another weird behavior. Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在我的具体情况下,这是我修复它的方法:
如您所见,在可调整大小的开始事件中,将位置设置为相对(通过同时使用可拖动和可调整大小,添加绝对定位)并设置顶部和保留为 0 以防止有时看到的“跳跃”。在停止事件中,反转这些更改。适用于我的情况,YMMV。
In my specific case, here is what I did to fix it:
As you can see, in the resizable start event, set the position to relative (by using draggable and resizable in concert, an absolute positioning is added) and set the top and left to 0 to prevent the "jumping" that you sometimes see. In the stop event, reverse these changes. Works in my case, YMMV.
找到了一个更简单的解决方案(适合我的需求);
在 jquery-ui.css 中,设置所需的位置并添加 !important 以确保其保持不变:
Found a simpler solution (that fits my needs);
Inside jquery-ui.css, set the desired position and add !important to make sure it stays:
使用此代码可将元素保留在窗口空间内,而无需在拖动时滚动:
$("#draggable").draggable({containment:"window",scroll: false})
Use this code to keep the element within the window space without scrolling while dragging:
$("#draggable").draggable({containment:"window", scroll: false})
Chrome 开发者工具指示 jQuery UI 在用户拖动/调整大小时使用
style="position:relative"
属性。这将覆盖 CSS 中的任何.demo{position:absolute}
。解决方法是设置您自己的
style="position:absolute"
(您可以使用 jQuerycss()
方法来实现此目的),然后为每个 div 显式设置顶部和左侧。Chrome Developer Tools indicates that jQuery UI uses the
style="position:relative"
attribute while the user is dragging/resizing. This will override any.demo{position:absolute}
in the CSS.The workaround is to set your own
style="position:absolute"
(you can use jQuerycss()
method for this) then explicitly set top and left for each div.只需将 #container 设置为
position:relative;
即可。 http://jsfiddle.net/g7Cgg/1/Just set the #container to
position: relative;
. http://jsfiddle.net/g7Cgg/1/