0

次の jQuery プラグインを作成しました。

(function($){
    $.fn.imageSlide = function(options){
      $(this).imageSlide.nextSlide();
      console.log("imageslide");
    };

    $.fn.imageSlide.nextSlide = function(){
      console.log("nextslide");
      $this = $(this);
    };

})(jQuery);

背景:

背景をクロスフェードするための画像スライダー プラグインが必要です (パフォーマンス上の理由から、Supersizedプラグインは使用できません)。いくつかの機能をユーザーに公開したいと思います: imageSlide プラグインの「コンストラクター」とimageSlide.nextSlideimageSlide.previousSlideユーザーがプラグインの外部からこれらのアクションを実行できるようにするなどの他のいくつかの機能。

この関数は、最初の画像をスライドイン (またはフェードイン) するimageSlideために を呼び出す必要があります。imageSlide.nextSlide function

問題:

行が関数$this = $(this);の無限再帰をトリガーしているようimageSlide.nextSlideです。

  • なぜこうなった?
  • $.fn.imageSlide.nextSlide = function(){};jQuery プラグインで別の関数を公開するのは正しい方法ではないようです。どうすればいいですか?
4

1 に答える 1

0

エラーの原因は正確にはわかりませんが、すべての静的メソッドを jQuery プロトタイプに入れる必要はありません。

次のようなものを使用してプラグインを公開してみてください。

(function($) {

// The constructor of your plugin:
var imageSlide = function(elems, options) {
    this.elems = elems; // the targets from the jQuery selector
    this.options = options;
};

// the public inherited methods:
imageSlide.prototype = {
    nextSlide: function() {
        console.log('nextSlide called');
    }
};

// extending the jQuery prototype and returning only one instance:
$.fn.imageSlide = function(options) {
    return new imageSlide(this, options);
};

})(jQuery);

これで、プラグインを呼び出すことができます。これは次のようなメソッドです。

var myGallery = $('#gallery').imageSlide();
myGallery.nextSlide();
于 2010-07-25T20:05:46.843 に答える