jQuery 可拖动恢复和停止事件
第一个问题,所以要友善:)
我想做的是在用户释放可拖动对象时以及恢复动画完成之前调用一个函数。
据我所知,仅当恢复完成时才会调用停止事件。我尝试将函数传递给可拖动的恢复选项,但它似乎不起作用。这是我的一些代码来演示;
$("a").draggable({
helper:function(){
return $("<div/>",{id:"mydrag",text:"link"}).appendTo("body");
},
revert:function(evt,ui){
// $("#mydrag").fadeOut("slow");
return true;
},
stop:function(evt,ui){
console.log("fin");
}
});
如果我取消注释恢复函数的第一行 - 淡出 - 那么该元素会淡出但不会恢复。当恢复动画完成时,控制台仅记录“fin”。
谁能帮助我吗?不用说,我在谷歌上搜索了很多答案,但没有运气。
巴斯特
First question, so be kind :)
What I am trying to do is call a function when the user releases the draggable, and before the revert animation has completed.
As far as I can see, the stop event is called only when the revert has finished. I have tried passing a function to the revert option of draggable, but it doesn't seem to work. Here is a bit of my code to demonstrate;
$("a").draggable({
helper:function(){
return $("<div/>",{id:"mydrag",text:"link"}).appendTo("body");
},
revert:function(evt,ui){
// $("#mydrag").fadeOut("slow");
return true;
},
stop:function(evt,ui){
console.log("fin");
}
});
If I uncomment the first line of the revert function - fadeout - then the element fades out but does not revert. The console only logs "fin" when the revert animation has completed.
Can anyone help me? Needless to say I have Googled a lot for an answer, but with no luck.
Buster
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,根据此博客,未记录的
revert 回调仅采用单个参数(丢弃套接字对象,或者如果丢弃被拒绝,则为布尔值
false
)。现在,问题是
draggable
在内部使用animate()
来重新定位助手。显然,该动画会排队,并且仅在fadeOut
效果完成后才开始。实现您想要的效果的一种方法是自己执行对 animate() 的调用,如下所示:
这允许可拖动帮助器在恢复时淡出。
您可以在此处测试该解决方案。
First, according to this blog, the undocumented
revert
callback only takes a single argument (the drop socket object, or the booleanfalse
if the drop was rejected).Now, the problem is that
draggable
usesanimate()
internally to reposition the helper. Apparently, that animation gets queued and only starts after yourfadeOut
effect finishes.One way to achieve what you want is to perform the call to
animate()
yourself, like this:That allows the draggable helper to fade out while it's being reverted.
You can test that solution here.