使用实时事件进行 jQuery 拖放
我有一个应用程序,其中有一个经常更改的长列表,并且我需要该列表中的项目可拖动。
我一直在使用 jQuery UI 可拖动插件,但添加到 400 多个列表项时速度很慢,并且每次添加新列表项时都必须重新添加。
有谁知道类似于 jQuery UI 可拖动插件的插件,它使用 jQuery 1.3 的 .live()
事件?这将解决这两个问题。
I have an application with a long list that changes frequently, and I need the items of that list to be draggable.
I've been using the jQuery UI draggable plugin, but it is slow to add to 400+ list items, and has to be re-added every time new list items are added.
Does anyone know of a plugin similar to the jQuery UI draggable plugin that uses jQuery 1.3's .live()
events? This would solve both problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Wojtek 的解决方案非常适合我。我最终对其进行了一些更改以使其扩展 jQuery...
现在不要像这样调用它:
...只需使用:
Wojtek's solution worked perfectly for me. I wound up changing it a tad bit to make it extend jQuery...
Now instead of calling it like:
...just use:
这是一个非常适合我的代码示例
This is a sample of code that perfectly worked for me
你可以像这样制作包装函数:(
我使用 jQuery 的原型 - 这就是我放置 jQuery() 而不是 $() 的原因)
现在使用 liveDraggable(selector, {opts}) 而不是 $(selector).draggable({opts}) })
You could make wrapper function like this:
(I use prototype with jQuery - that's why i placed jQuery() instead of $())
And now instead of $(selector).draggable({opts}) use liveDraggable(selector, {opts})
Stldoug 的代码对我有用,但无需在每个鼠标悬停事件上继续检查元素的 .data("init") 。另外,最好使用“mousemove”,因为当 .live 函数启动时,如果鼠标已经位于元素上方,则“mouseover”并不总是会被触发。
以下是使用它的方法:
技巧是添加“:not” (.ui-draggable)”到您的选择器。由于当元素变得可拖动时,jQuery 会自动将“ui-draggable”类添加到元素中,因此 .live 函数将不再以它为目标。换句话说,它只触发一次,不像其他解决方案,当你移动东西时会一遍又一遍地触发。
理想情况下,您可以只取消绑定“mousemove”,但不幸的是,这不适用于 .live。
Stldoug's code worked for me, but there's no need to keep checking the element's .data("init") on every mouseover event. Also, it's better to use "mousemove", as "mouseover" doesn't always get triggered if your mouse is already over the element when the .live function kicks in.
Here's how you use it:
The trick is to add ":not(.ui-draggable)" to your selector. Since jQuery will automatically add the "ui-draggable" class to your element when it becomes draggable, the .live function will no longer target it. In other words, it only triggers once, unlike the other solution which triggers over and over as you move stuff around.
Ideally, you could just .unbind the "mousemove", but that doesn't work with .live, unfortunately.
结合 @john 和 @jasimmk 的最佳答案:
使用
.live
:.live
已弃用,最好使用.on
:正如 @john 所解释的,
.ui-draggable
会自动添加到可拖动方法中,因此通过使用选择器排除该类,您可以确保draggable()每个元素只会被调用一次。并且使用.on
将缩小选择器的范围,从而提高性能。Combining the best answers from @john and @jasimmk:
Using
.live
:.live
is deprecated though, better to use.on
:As @john explained,
.ui-draggable
is automatically added to draggable methods, so by excluding that class with the selector, you ensure that draggable() will only be called once on each element. And using.on
will reduce the scope of the selector, improving performance.示例:
土耳其语:
英语:
注意:您可以使用
on()
代替live()
或delegate
。on()
比其他方法具有更好的性能An example:
Turkish:
English:
Note: You can use
on()
instead oflive()
ordelegate
. Theon()
has good performance than othersJSFiddle
JSFiddle
一个老问题。但 Threedubmedia 具有拖放插件,支持实时(从 v 1.7 开始,简称为“on”)支持。
http://tridubmedia.com/code/event/drop
没有太多使用它,所以我无法解释它的性能等,但看起来很合理。
An old question. But threedubmedia has drag and drop plugin with live (as of v 1.7 known as simply "on") support.
http://threedubmedia.com/code/event/drop
Haven't used it to much so I can't account for it performance, etc. but looks reasonable.
另一种选择是将鼠标悬停处理程序与可移动类混合使用,如下所示:
它相当简单,并且解决了其他答案在鼠标悬停时一遍又一遍地重新绑定所带来的一些问题。
Another option is to mix the mouseover handler with a removable class, like so:
It's fairly straightforward and resolves some of the issues that other answers have with re-binding over and over as you mouseover.
不使用 live 的更新版本,因为它已被弃用:
An updated version that does not use live as it is deprecated: