jQuery,保存多个可拖动对象的原始位置以将它们恢复回来
请参阅 jsfiddle
一旦我将“可拖动”对象放在目标上,就会使用该特定位置下次回来的时候。有什么方法可以保存可拖动对象的原始位置并始终将它们恢复到原来的位置(第一个位置)?或者除了“拯救他们”之外还有其他解决方案来达到同样的目的吗?
我发现有些类似 问题,但位置(左、上)被硬编码在其中以恢复。我努力尝试但找不到解决方案。
谢谢。
Please see the jsfiddle
Once I drop the 'draggable' object on the target, that particular position is used next time, when reverting back. Is there any way by which I can save the original positions of the draggable objects and always revert them back to them (the very first positions)? Or is there any other solution than 'saving them' to achieve the same goal?
I found somewhat similar question, but the positions(left, top) are hardcoded in them to revert back. I tried hard but couldn't find a solution.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设当您将可拖动对象放在目标上时,您不希望它恢复,但是在将其移回目标之外后,您希望它与其余部分一起恢复到其原始位置可拖动对象?
如果是这种情况,此答案中的代码就是您要查找的内容,尽管值
0 , 0
表示顶部和左侧位置似乎是硬编码的,它们是必要的,因为可拖动对象的position
CSS 属性在它们被设置后被设置为relative
变成Draggables,因此将它们设置回0, 0
会将它们移回原来的位置。这是一个 jsfiddle 示例,以及示例中修改后的代码:
希望这有帮助!如果我完全没有达到目标,请告诉我!
I'm assuming that when you drop your draggable on the target, you don't want it to revert, but after you move it back out of the target, you'd like it to revert back to its original position with the rest of the draggables?
If that's the case, the code in this answer is what you're looking for, and although the values
0, 0
for the top and left locations seem hard-coded, they are necessary as the draggables'position
CSS attributes get set torelative
after they've been turned into draggables, so setting them back to0, 0
would move them back to their original positions.Here's a jsfiddle example, and also the modified code in your example:
Hope this helps! Please let me know if I've missed the mark completely!
Chris Kempen 的答案是正确的,但在较新版本的 jQuery 中,您可能需要
在“out”回调中使用,因为数据属性已重命名。
Chris Kempen's answer is correct, but in newer versions of jQuery you might want to use
in the "out"-callback, as the data attribute was renamed.