2012-11-29 118 views
0

我有一個jQuery.pep(拖放jQuery插件)的JavaScript文件。我開始插入此代碼:如何禁用/停止jQuery功能?

Drag.pep(); 

現在,我不知道如何禁用/停止它。以下是文件中的代碼部分:jquery.pep.js。我認爲它可以幫助你解答:

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

變量「pluginName」是「打氣」。這裏是full jQuery pep code(github):

請幫忙。

謝謝!

回答

3

,可能應該這樣做:

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

編輯:

代碼

是有可能的實例對象上調用函數:

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

實例建此處並保存到數據屬性中:

// 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() {}; 
+0

首先,非常感謝你!這是正確的,唯一的一點是,它不只停止我的當前對象 - Drag.pep() - ,它也停止所有其他對象 - Drag1.pep(),Drag2.pep()... - 任何想法?對不起,如果我被誤解了......再次感謝你! – EranLevi

+0

編輯我的評論,如果你想這更乾淨你可能應該把更多的代碼插入插件,這應該不是那麼困難(添加一個啓用/禁用布爾值的實例 – hereandnow78

+0

WOW !!!我真的很感激你的幫助!你是最好的!我現在正在測試它...... :) – EranLevi