如何在自定义帮助程序中重新实现 jQuery 的默认帮助程序

How to reimplement jQuery's default helper in a custom helper

本文关键字:帮助程序 实现 jQuery 默认 新实现 自定义      更新时间:2023-09-26

我正在创建一个自定义的拖动助手(在jQuery中):

$('.dragme', element).draggable({
    appendTo: 'body',
    helper  : custom_drag_helper,
    opacity : 0.5
});

我这样做是因为我想有时克隆,有时做默认功能,即拖动原始元素。

function custom_drag_helper() {
    if (/*criteria on when to move instead of clone */) {
        return $(this); /* this is what helper: 'original' seems to do */
    } else {
        clone = $(this).clone(); /* this is what helper: 'clone' does */
        return clone;
    }
}

但是我根本无法让原始功能工作。 返回克隆()工作正常,但返回$(这)不会带来快乐。

好的,在输入这个问题时,我做了更多的源潜,发现了这个小行:

if (this.options.helper == 'original' && !(/^(?:r|a|f)/).test(this.element.css("position")))
        this.element[0].style.position = 'relative';

在我试图解决这个问题的一天里,我没有发现。在上面的代码中添加this.style.position = 'relative';解决了问题!