如何禁用/停止jQuery功能?

时间:2012-11-29 16:08:03

标签: javascript jquery drag-and-drop

我有一个jQuery.pep(拖放jQuery插件)的JavaScript文件。我正在使用此代码启动插件:

Drag.pep();

现在,我不知道如何禁用/停止它。以下是文件中的代码部分:jquery.pep.js。我想它可以帮助你回答:

$[pluginName] = {
  stopAll: function () {
    disable = true;
    return this;
  },
  startAll: function () {
    disable = false;
    return this;
  }
};

变量“pluginName”是“pep”。这是full jQuery pep code(github):

请帮忙。

谢谢!

1 个答案:

答案 0 :(得分:3)

应该这样做:

$.pep.stopAll(); // stops all peps

编辑:

在代码中有一个可以在实例对象上调用的函数:

Pep.prototype.forceStop = function(){
  $(this.el).trigger( this._endTrigger );
};

实例在此处构建并保存到data-attribute中:

// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
    return this.each(function () {
        if (!$.data(this, 'plugin_' + pluginName)) {
            $.data(this, 'plugin_' + pluginName, new Pep( this, options ));
        }
    });
};


$('your-selector').data('plugin_pep').forceStop();

我试图这样做,它调用了forceStop方法,但它最终没有这样做。我研究并认为它的作用是阻止当前正在进行的动画。

你现在可以做的是覆盖拖拽方法,当你需要时可以用选项给出,并且当动画被触发时立即强制停止。

我不喜欢那么多,但是正如我现在看到的代码,我没有看到任何机会以另一种方式做到这一点(这里缺少文档; - ()

这应该有效:

var myPep = $('your-selector').data('plugin_pep');
myPep.options.drag = function(ev, obj) {
  obj.forceStop();
};

edit2:如果你想让它再次拖动,只需用空函数重新覆盖该函数:

myPep.options.drag = function() {};