2011-12-29 17 views
-5

私はJQtouch用のスライダーの仕組みに取り組んでいました。プラグインには2つの方法があります.1つは開始時にスライダーを構築し、もう1つは回転で再構築する方法です。現在のスライドの番号を保持する "count"という変数を使用します。私の問題は、モバイルデバイスが回転し、「回転」メソッドが呼び出されたときに変数を失うことです。jQueryプラグインのグローバル変数

自分の設定でcount変数を "1"と定義しました。これは、rotateメソッドが呼び出されたときに、現在のスライド番号の代わりにデフォルトの "1"が設定されているようです。ここに私のプラグインのコードです。それは非常に長いです。基本的に私のinit:メソッドで起こっていることはすべて、 "count"変数のスワイプおよびスライドの変更はすべて "rotate"メソッドに引き継がれません。

(function ($) { 
    var empty = {}; 
    var settings = { 
     count: 1, 
    }; 
    var methods = { 
     init: function (options, callback, defaults) { 
      var options = $.extend({}, 
      settings, options || {}); 
      return this.each(function() { 
       var $this = $(this); 
       if (options.height) { 
        mHeight = options.height; 
       } else { 
        mHeight = $(window).height(); 
       } 
       mWidth = $(window).width(); 
       mAmount = $('.swiper', this).length; 
       amount = $('.swiper', this).length; 
       holdWidth = mWidth * options.amount; 
       $('.swiper', this).height(mHeight); 
       $(this).height(mHeight); 
       $('.swiper', this).width(mWidth); 
       $(this).width(holdWidth); 
       $('.swipe_slide', this).width(holdWidth); 
       $('.swipe_slide', this).height(mHeight); 
       $('.swiper', this).each(function (i) { 
        var nwidth = mWidth * i; 
        $(this).css('left', nwidth); 
       }); 
       $('.swipe_slide', this).swipe(function (evt, data) { 
        var amount = $('.swiper', this).length; // alert($($this).attr('id')); 
        if (data.direction == 'left') { //alert('count: '+options.count+" amount: "+options.amount); 
         if (options.count != amount) { 
          moveWidth = options.count * -mWidth; 
          $('.swipe_slide', $this).css("left", moveWidth); 
          options.count++ 
         } else { 
          return 
         } 
        } else { 
         if (options.count != 1) { 
          moveWidth = moveWidth + mWidth; 
          $('.swipe_slide', $this).css("left", moveWidth); 
          options.count-- 
         } else { 
          return 
         } 
        } 
       }); 
      }); 
     }, 
     rotate: function (options, callback, defaults) { 
      var $this = $(this); 
      var options = $.extend({}, 
      settings, options || {}); 
      alert(options.count); 
      return this.each(function() { 
       if (options.height) { 
        mHeight = options.height; 
       } else { 
        mHeight = $(window).height(); 
       } 
       mWidth = $(window).width(); 
       mAmount = $('.swiper', this).length; 
       amount = $('.swiper', this).length; 
       holdWidth = mWidth * mAmount; 
       $('.swiper', this).height(mHeight); 
       $(this).height(mHeight); 
       $('.swipe_slide', this).height(mHeight); 
       $('.swiper', this).width(mWidth); 
       $(this).width(holdWidth); 
       $('.swipe_slide', this).width(holdWidth); 
       $('.swiper', this).each(function (i) { 
        var nwidth = mWidth * i; 
        $(this).css('left', nwidth); 
       }); 
       newMoveWidth = options.count * $(window).width(); 
       alert(options.count); 
       alert(newMoveWidth); //$('.swipe_slide',$this).css("left" , 0); 
      }); 
     } 
    } 
    $.fn.jCarousel = function (method, options) { 
     if (methods[method]) { 
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
     } else if (typeof method === 'object' || !method) { 
      return methods.init.apply(this, arguments); 
     } else { 
      $.error('Method ' + method + ' does not exist on jQuery.jModalbox'); 
     } 
    }; 
})(jQuery); 
+2

が悪いインデント=言うことができない場所あなたの余分な '' '}' ''です。 –

+0

私はそれをコピーして貼り付けたときに何か落ちると感じました。私は、コピーと貼り付けに影響を与えているかもしれないプラグインには何の影響も与えない軽薄なコメントやものを削除しようとしました。 –

+0

私はあなたのコードをhttp://jsbeautifier.com/でブックマークしています。 –

答えて

0

私はこれに従った場合、あなたの問題は、あなたがextendedオプション、ローカル変数にcountの値を変更しているです。 settingsをターゲットとして渡さない限り、これらの変更はsettingsまで反映されません。それはまさにあなたが望むものではないかもしれませんが、私はそれはあなたが正しい方向に向かって取得する必要がありますかなり確信している:

http://jsfiddle.net/DTnW8/