在 jQuery sortable() 之后调用自定义函数;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14821033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:57:52  来源:igfitidea点击:

Call custom function after jQuery sortable();

jqueryjquery-ui

提问by ndesign11

I have this little plugin that causes an element to pulsate three times calling a .blink(). I am trying to make it work on the LI element after it has been dragged and dropped via jQuery .sortable(); but it doesn't seem to work in this context.

我有这个小插件,它会调用 .blink() 使元素脉动三次。在通过 jQuery .sortable() 拖放 LI 元素后,我试图让它在 LI 元素上工作;但它似乎在这种情况下不起作用。

$(function() {
$( "#sortable" ).sortable().blink("update-value", 3, 350);
$( "#sortable" ).disableSelection();
});

BLINK CODE

闪码

$.fn.blink = function (cls, times, delay) {
var $self = this.removeClass(cls);
clearTimeout($.fn.blink.handler);
! function animate(times) {
    if (times) {
        $self.toggleClass(cls);
        $.fn.blink.handler = setTimeout(function () {
            animate(times - 1);
        }, delay);
    }
}(times * 2);
return this;
};

回答by MatRt

If you are using the jQueryUI sortable plugin, and if you want to make a blinking element that has been dragged and dropped (sorted), may be you should use the callbacks that are already available in the sortable API:

如果您正在使用 jQueryUI 可排序插件,并且如果您想制作一个已被拖放(排序)的闪烁元素,可能您应该使用可排序 API 中已经可用的回调:

When you configure your sortable, you can give a callback for the change event:

当您配置 sortable 时,您可以为 change 事件提供回调:

$( ".selector" ).sortable({
    change: function( event, ui ) {}
});

API says: "This event is triggered during sorting, but only when the DOM position has changed"

API 说:“此事件在排序期间触发,但仅在 DOM 位置发生变化时触发”

You can also give a callback for the update event:

您还可以为更新事件提供回调:

$( ".selector" ).sortable({
    update: function( event, ui ) {}
});

API says: "This event is triggered when the user stopped sorting and the DOM position has changed"

API 说:“当用户停止排序并且 DOM 位置发生变化时触发此事件”

In your case, you should use the update callback and call the blink method to your element.

在您的情况下,您应该使用更新回调并调用您的元素的闪烁方法。

Note: the element drag and dropped should be available in the uiobject, use a console.debug to check the content of the ui

注意:元素拖放应该在ui对象中可用,使用console.debug检查内容ui

回答by Clint Decker

For those having trouble, I use "stop" instead of change or update. When I use update, it runs before the item is dropped, so when I access the dom to get the order, it hasnt updated it. When I use change, it randomly triggers mid drag. I was only able to get it to work by using stop: function( event, ui ) {}

对于那些遇到问题的人,我使用“停止”而不是更改或更新。当我使用更新时,它会在项目被删除之前运行,所以当我访问 dom 以获取订单时,它并没有更新它。当我使用更改时,它会随机触发中拖。我只能使用 stop: function( event, ui ) {}