jQuery Sortable - 删除拖动时的默认行为,放置后恢复
我有一个图片网格,我希望能够拖放它们进行排序。 sortable() 效果很好。需要注意的是,我还运行了 jQuery Fancybox 插件 http://fancybox.net ,这将打开一个更大的单击时的图像视图。
所以你可以点击开始拖动,但是一旦你放下,fancybox就会被触发并且图像会放大。
我已经尝试过类似的事情,但事情真的变得一团糟(拖动根本不再起作用,我的列表在整个页面上重新排列,这很糟糕):
$('#list').sortable({
start:function(e,ui) {
e.preventDefault();
}
});
尝试使用 start
和 <代码>停止但没有运气。我还尝试解除项目的 click
处理程序的绑定,但这也没有帮助。理想情况下,我可以在拖动时“隐藏” fancybox 处理程序,然后在放下后恢复它们。可能有一种方法可以在拖动时取消整个列表的 Fancybox,然后再次调用 $.fancybox()
来启动它,但必须有一种更简单的方法...
感谢您的帮助!
I've got a grid of pictures that I want to be able to drag/drop to sort. sortable() works great. The caveat is that I've also got the jQuery Fancybox plugin http://fancybox.net running which will open up a larger view of an image when clicked.
So you can click to start dragging, but as soon as you drop fancybox is triggered and the image enlarges.
I've tried something like this, but things get REALLY messed up (drag doesn't work at all anymore, my list gets rearranged all over the page, it's nasty):
$('#list').sortable({
start:function(e,ui) {
e.preventDefault();
}
});
Tried with both start
and stop
but no luck. I've also tried unbinding the click
handler for the items, but that didn't help either. Ideally I could sort of "hide" the fancybox handlers while dragging and then reinstate them after I drop. There may be a way to un-fancybox the whole list while dragging and then call $.fancybox()
again to start it up, but there's gotta be an easier way...
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我成功了!我确信下面的 kwicher 可以工作,但我必须稍微调整一下。我最终不得不在 fancybox 回调和可排序回调之间来回交换:
将
removeClass()
放在可排序的stop
回调中不起作用:显然sortablestop
在 fancyboxonStart
之前触发,因此当 fancybox 去查看时,类已经消失了。请注意,sortable 直接针对列表中的
,但 fancybox 的目标是列表项内的
。这就是为什么我必须在 fancybox 的
onStart
中获取parent()
。哦,只需在onStart
中返回 false 就足以让 fancybox 忽略该点击!All right, I got it working! I was sure that kwicher's below would work but I had to tweak it a bit. I ended up having to swap back and forth between the fancybox callbacks and the sortable callbacks:
Putting the
removeClass()
in thestop
callback of sortable wasn't working: apparently the sortablestop
fires before the fancyboxonStart
and so the class was gone by the time fancybox went to look.Note that the sortable targets the
<li>
in the list directly, but fancybox targets the<a>
inside the list item. That's why I have to get theparent()
in fancybox'sonStart
. Oh, and simply goingreturn false
in theonStart
is enough to get fancybox to ignore that click!我用一个相当低技术含量的项目方法解决了这个问题。在 start 内部,我会标记一些内容,表示当前单击与拖动相关,应该被忽略。
然后,在正常单击事件触发的代码中,我将检查当前单击是否被标记为用于拖动。超低技术示例:
显然,使用全局变量并不理想,但这演示了这个概念。我想知道是否可以将单击事件的某些属性标记为拖动单击。也许我稍后会做一些摆弄,然后看看。
我还在 Stack Overflow 上看到了另一篇文章,该文章处理了类似的问题并使用了解除绑定和重新绑定:
jQuery UI可排序 -- 如何取消拖动/排序的项目上的点击事件?
I solved this problem with a fairly low tech approach for a project. Inside of start I would mark something saying that the current click is related to a drag and should be ignored.
Then in the code that gets triggered for a normal click event I would check to see if the current click was marked as for dragging. Super low tech example:
Obviously, using globals is not ideal, but at this this demonstrates the concept. I am wondering if some property of the click event can be marked to say that its a dragging click. Maybe I will do some fiddling later and see.
I also saw another post on Stack Overflow that dealt with a similar issue and used unbinding and rebinding:
jQuery UI Sortable -- How can I cancel the click event on an item that's dragged/sorted?
我还没有测试过它,但原则上应该可行:
你可以尝试一下。
K
I have not tested it yet but in principle that should work:
You could give it a try.
K