jQuery 可拖动 + droppable:如何将放置的元素捕捉到放置的元素
我将屏幕分为两个 DIV
。 在左侧的 DIV
中,我有一些 50x50 像素的 DIV
,在右侧的 DIV
中,我有一个由 80x80 LI 组成的空网格
。 左侧的 DIV
是可拖动的,一旦放到 LI
上,它们应该捕捉到该 LI
的中心。
听起来很简单,对吧? 我只是不知道如何完成这件事。 我尝试通过操作删除的 DIV
的 top
和 left
CSS 属性来匹配 LI
它们的属性重新放入,但 left
和 top
属性是相对于左侧 DIV
的。
如何才能最好地将放置的元素捕捉到它所放置的元素的中心? 这一定很简单,对吧?
编辑:我正在使用 jQuery UI 1.7.2 和 jQuery 1.3.2。
编辑2:对于其他遇到此问题的人,这就是我修复它的方法:
我使用 Keith 的解决方案,删除拖动的元素并将其放置在 drop droppable 插件的回调:
function gallerySnap(droppedOn, droppedElement)
{
$(droppedOn).html('<div class="drop_styles">'+$(droppedElement).html()+'</div>' );
$(droppedElement).remove();
}
我不想让放置的元素再次可拖动,但如果你这样做,只需再次将可拖动元素绑定到它即可。
对我来说,此方法还解决了定位放置的元素(相对于左侧 DIV
)并在第二个 DIV
内滚动时遇到的问题。 (元素将保持固定在页面上,现在它们会滚动)。
我确实使用了对齐选项,使其在拖动时看起来不错,所以感谢 karim79 的建议。
我可能不会因此赢得任何很棒的代码奖,所以如果您发现有改进的空间,请分享!
I have my screen divided into two DIV
s. In the left DIV
I have a few 50x50 pixel DIV
s, in the right DIV
I have an empty grid made of 80x80 LI
s. The DIV
s on the left are draggable, and once dropped on a LI
, they should snap to center of that LI
.
Sounds simple, right? I just don't know how to get this done. I tried by manipulating the dropped DIV
's top
and left
CSS properties to match those of the LI
they're dropped into, but the left
and top
properties are relative to the left DIV
.
How can I best have the dropped element snap to the center of the element it's dropped into? That's gotta be simple, right?
Edit: I'm using jQuery UI 1.7.2 with jQuery 1.3.2.
Edit 2: For whoever else has this problem, this is how I fixed it:
I used Keith's solution of removing the dragged element and placing it inside the dropped-on element in the drop
callback of the droppable plugin:
function gallerySnap(droppedOn, droppedElement)
{
$(droppedOn).html('<div class="drop_styles">'+$(droppedElement).html()+'</div>' );
$(droppedElement).remove();
}
I don't the dropped element to be draggable again, but if you do, just bind draggable to it again.
For me this method also solved the problem I had when positioning the dropped elements (which would be relative to the left DIV
) and scrolling inside the second DIV
. (Elements would remain fixed on page, now they scroll along).
I did play with the snap options to make it look good while dragging, so thanks to karim79 for that suggestion.
I probably won't win any Awesome Code prizes with this, so if you see room for improvement, please share!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我发现基思的方法对我有用。 由于他的答案不包括示例实现,我将发布我的:
或者,稍微更简洁一些:
I found that Keith's method worked for me. Since his answer doesn't include an example implementation, I'll post mine:
or, slightly more concisely:
我遇到了类似的问题 - 我通过手动从其旧父级中删除拖动的元素并将其添加到放置的元素中来解决它。
I had a similar problem - I worked around it by manually removing the dragged element from its old parent and adding it to the dropped on element.
感谢您的帖子 - 它帮助我朝正确的方向发展。 我发现设置可拖动对象的位置属性比弄乱 HTML 代码要干净一些。 这会将位置设置为可放置对象的左上角,但您也可以修改以使其居中。
Thanks for your post - it helped me in the right direction. I find it a bit cleaner to set the position properties of the draggable object instead of messing with the HTML code. This sets the position to the top left corner of the
droppable
object, but you can modify to have it centered as well.我发现当您进行拖动时,jQuery UI 会添加一个内联来告诉您将其放置在何处。 下面是我用来将其固定到位的代码示例
I found that when you do the drag, jQuery UI adds an inline to tell you where you dropped it. Below is a sample of the code that I used to snap it into place
如果左侧的
div
实际上位于右侧的li
中(您可以使用 Firebug 确认),并且如果li
全部位于ul
中(它们应该是这样),请尝试以下一项或两项操作:If the
div
s on the left are actually in theli
s on the right (which you can confirm with Firebug), and if theli
s are all in aul
(as they should be), try one or both of the following:根据巴里的代码,如果我们想添加一个带有“x”按钮的选项,该元素将再次从新的父元素中分离并重新附加到初始元素上,该怎么办?
我是这样想的,但似乎没用。
制作某种保持初始状态的变量
肯定是错误的,但我不知道你是否能理解这个概念......
只是能够将你已经掉落的任何东西恢复到初始状态。
based on Barry's code, what if we d like to add an option with an "x" button the element to be detached again from the new parent and be reattached to the initial?
i thought sth like this, but didn't seem to work..
to make some sort of a variable that hold initial state
sth is definately wrong but i don't know if you can get the concept...
just being able to reverse whatever you have dropped to their initial state.