请推荐一个JQuery插件来处理可拖动元素的冲突检测 [英] Please recommend a JQuery plugin that handles collision detection for draggable elements
问题描述
我们正在使用可拖动 JQuery UI插件,并且需要禁止在我们的元素之间重叠。我们自己可以编写一些碰撞检测,但更喜欢使用经过测试的包。有什么建议?
We're using the Draggable JQuery UI plugin and need to disallow overlapping among our elements. We could write some collision detection ourselves but would prefer to use a tested package. Any suggestions?
推荐答案
你可以试试 jquery-collision 加上 jquery-ui-draggable-collision 。完全披露:我刚刚在sourceforge上写了并发布了这些。
You can try jquery-collision plus jquery-ui-draggable-collision. Full disclosure: I just wrote and released these on sourceforge.
第一个允许这个:
var hit_list = $("#collider").collision(".obstacle");
这是与#collider重叠的所有.obstacle的列表。
which is the list of all ".obstacle" that overlap "#collider".
第二个允许:
$("#collider").draggable( { obstacle: ".obstacle" } );
这给你(除其他外)一个碰撞事件要绑定到:
Which gives you (among other things), a "collision" event to bind to:
$("#collider").bind( "collision", function(event,ui){...} );
您甚至可以设置:
$("#collider").draggable( { obstacle: ".obstacle", preventCollision: true } );
防止#collider在拖动时与任何.obstacle重叠。
to prevent "#collider" from ever overlapping any ".obstacle" while dragging.
这篇关于请推荐一个JQuery插件来处理可拖动元素的冲突检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!