Variables are being shared across instances

Variables are being shared across instances

Hi. I'm making a image slider plugin which is working fine with single instance but when I have multiple instances on the page, the variables are being shared across these instances. How do I sandbox them and keep the instances isolated? Here's the code:

  1. var slider = $('.slider').mySlider();

  1. ;(function ($) {
  2.   var defaults = {
  3.     auto: false,
  4.     delay: 4000,
  5.     controls: true,
  6.     controlsPosition: 'inside', //inside, outside, neighbors
  7.     prevHtml: '<i class="material-icons">chevron_left</i>',
  8.     nextHtml: '<i class="material-icons">chevron_right</i>',
  9.     width: 70,
  10.     transitionSpeed: 400,
  11.     backdrop: true,
  12.     perspective: false,
  13.     pauseOnHover: true,
  14.     keyboard: true,
  15.     onLoad: function() {},
  16.     onSlideEnd : function() {}
  17.   };

  18.   var mySliderInit = function (el, options) {
  19.     var settings = $.extend({}, defaults, options || {}),
  20.     el = $(el);
  21.     el.wrap('<div class="mySlider-outerwrapper"><div class="mySlider-wrapper"></div></div>');

  22.     var outerWrapper = el.closest('.mySlider-outerwrapper'),
  23.     wrapper = el.closest('.mySlider-wrapper');

  24.     var numSlides = el.find('li').length,
  25.     numElements = numSlides+4,
  26.     wrapperWidth = wrapper.width(),
  27.     slideWidth = wrapperWidth*(settings.width)/100,
  28.     sideWidth = wrapperWidth*((100 - settings.width)/2)/100;

  29.     var first_slide = el.find("li").slice(0,2),
  30.     last_slide = el.find("li").slice(-2);
  31.     el.prepend(last_slide.clone().addClass('mySlider-clone'));
  32.     el.append(first_slide.clone().addClass('mySlider-clone'));
  33.     el.width(numElements*slideWidth);
  34.     var slides = el.find('li');
  35.     slides.width(slideWidth);

  36.     //first move
  37.     var index = 0;
  38.     var slideMovement = wrapperWidth*settings.width/100;
  39.     var firstMovement = currentPosition = -(slideWidth-sideWidth+slideWidth);
  40.     el.css('transform', 'translateX('+(firstMovement)+'px)');

  41.     if(settings.controls){
  42.       outerWrapper.append('<a href="#" class="mySlider-nav mySlider-prev">'+settings.prevHtml+'</a>');
  43.       outerWrapper.append('<a href="#" class="mySlider-nav mySlider-next">'+settings.nextHtml+'</a>');
  44.     }

  45.     function moveSlider(direction){
  46.       if(direction == 'forward'){
  47.         index++;
  48.         currentPosition -= slideWidth;
  49.       } else if(direction == 'backward'){
  50.         index--;
  51.         currentPosition += slideWidth;
  52.       }
  53.       $(slides[index+2]).addClass('active').siblings().removeClass('active');
  54.       el.css('transform', 'translateX('+currentPosition+'px)');
  55.       setTimeout(function() {
  56.         if(index > numSlides-1){
  57.           index = 0;
  58.           currentPosition = firstMovement;
  59.           var loop = true;
  60.         } else if(index < 0){
  61.           index = numSlides-1;
  62.           currentPosition -= numSlides*slideWidth;
  63.           var loop = true
  64.         } else {
  65.           var loop = false;
  66.         }
  67.         if(loop){
  68.           slides.css('transition-duration', '0ms');
  69.           $(slides[index+2]).addClass('active');
  70.           el.css({
  71.             'transition-duration': '0ms',
  72.             'transform': 'translateX('+currentPosition+'px)'
  73.           });
  74.           setTimeout(function() {
  75.             el.css('transition-duration', settings.transitionSpeed+'ms');
  76.             slides.css('transition-duration', settings.transitionSpeed+'ms');
  77.           }, 20);
  78.         }

  79.         settings.onSlideEnd.call(el);
  80.       }, settings.transitionSpeed);
  81.     }

  82.     outerWrapper.on('click', '.mySlider-next', function(e){
  83.       e.preventDefault();
  84.       moveSlider('forward');
  85.     });

  86.     outerWrapper.on('click', '.mySlider-prev', function(e){
  87.       e.preventDefault();
  88.       moveSlider('backward');
  89.     });

  90.     return this;
  91.   };

  92.   $.fn.mySlider = function (settings){
  93.     return this.each(function(){
  94.       new mySliderInit($(this), settings);
  95.     });
  96.   }

  97. }(jQuery));
Sorry about the long code but I believe the issue is somewhere in the organization of code itself.
Thanks