2011-10-26 2 views
1

私はプラグインを書くことを学んでいます。プラグインオーサリング用のjQueryサイトは混乱します。私は実際にブラウザイベントに基づいて起動するメソッドをプラグインに追加する方法を理解しようとしています。だから、基本的に私はモバイルデバイスのためのスワイプ可能なスライドショーを構築するプラグインを持っています。デバイスが回転したら、新しい寸法のスライドショーを再構築する必要があります。今、私はそれを初めて持っていますが、(a)異なる機能のメソッドを追加し、(b)それらの特定のメソッドにイベントをバインドすることはできません。ここに私のコードです。Jqueryプラグインの開発、ブラウザのイベントとメソッドを添付してください。

(function($){ 

    $.fn.jCarousel = function(options) { 
    var empty = {} 

    var defaults = { 
     height: $(window).height(), 
     width: $(window).width(), 
     count: 1, 
     amount: $('.swiper', this).length, 
     thisone:this, 
    }; 

    var options = $.extend(empty, defaults, options); 

    return this.each(function() { 

     options.thisone = this; 

     amount = $('.swiper', this).length; 
     holdWidth = options.width * options.amount; 

     $('.swiper', this).height(options.height); 
     $(this).height(options.height); 
     $('.swiper', this).width(options.width); 
     $(this).width(holdWidth); 
     $('.swipe_slide',this).width(holdWidth); 

     //================================================================================================================== 
     //================================================================================================================== 

     $('.swiper', this).each(function(i) { 
      var nwidth = options.width*i; 
      $(this).css('left',nwidth); 
     }); 
     //================================================================================================================== 
     //==================================================================================================================  
     //================================================================================================================== 
     //================================================================================================================== 

     $('.swipe_slide', this).swipe(function(evt, data) { 
      if(data.direction=='left'){ 
       //alert('count: '+options.count+" amount: "+options.amount); 
       if(options.count != options.amount){ 
        moveWidth = options.count * -options.width; 
        $('.swipe_slide',options.thisone).css("left" ,moveWidth); 
        options.count++ 
       }else{ 
        return  
       } 
      }else{ 
       if(options.count!=1){  
        moveWidth = moveWidth+ options.width; 
        $('.swipe_slide',options.thisone).css("left" ,moveWidth); 
        options.count-- 
       } 
       else{ 
        return 
       } 
      } 
     });  
     //==================================================================================================================  
     //================================================================================================================== 
     }); 
    }; 

})(jQuery); 

答えて

1
$(window).bind('orientationchange', $.fn.jCarousel); 

あなたはそれ以外の向きの変更で再度機能を実行すると、デフォルトにオプションをリセットします、機能がそれに渡されるオプションを格納するように、あなたのプラグインビットをリファクタリングする必要があります。

関連する問題