jQuery UI droppable 'drop' event to wait for revert

百般思念 提交于 2019-12-24 17:43:20

问题


I am developing some simple drag-drop interactions with jQueryUI. Under some conditions, I would like the dragged item to go back to its original place (revert) and then shake. I placed the shake in the drop event of the droppable object, but it doesn't seem to wait for the revert to be finished.

What's worse, sometimes it shakes before starting to revert to the original position, other times it shakes after the full revert has been done, and other times the shake gets caught up in the middle. It's not consistent.

Is there any way to execute the drop event of the droppable object after the draggable has completed its revert?

See this JSFiddle for an example: http://jsfiddle.net/sbQUE/

In other words, if I set up the droppable as follows:

this.$el.droppable({
    drop: someFunction
});

...the executed function is:

someFunction: function(ev, el){
    $(el.draggable).effect('shake');
}

... and the draggable object that will fall on the droppable is set up like:

this.$el.draggable({
    revert: true,
    revertDuration: 0 // or any number for that matter
});

How can I make sure the "someFunction" callback will be executed after the draggable is back in its original place thanks to the revert option?

Note: I tried looking for some sort of after revert callback or event but it doesn't exist (at least not in jQueryUI's documentation).


回答1:


If you can make jsfiddle for you example, I can apply this to your code, but generally this can be accomplished in jquery using system of promise:

You will have to do something like this:

javascript:

 $(function () {
     $("#draggable").draggable({
         revert: "valid"
     });

     $("#droppable").droppable({
         activeClass: "ui-state-hover",
         hoverClass: "ui-state-active",
         drop: function (event, ui) {
             setTimeout(function () {
                 $("#draggable").promise().done(function () {
                     $("#draggable").effect('shake', {}, 500);
                 });
             }, 100);
         }
     });
 });

Here is a solved jsfiddle

Incase you are wondering about the setTimeout before .promise() that is to make sure the revert animation has been activated. Then promise() will wait until it is executed.



来源:https://stackoverflow.com/questions/18316011/jquery-ui-droppable-drop-event-to-wait-for-revert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!