2016-12-01 60 views
1

我有一些jQuery,当一个传送带在页面上时效果很好。我需要在同一页面上放置两个轮播,目前无法使用。有人可以告诉我如何使其工作?与自定义jQuery在同一页上有两个传送带的问题

var Carousel = (function($) { 
    var $carousel = $('.carousel'); 
    var activeItemIndex = 0; 
    var s; 

    return { 
     settings: { 
      delay: 10000, 
      items: $carousel.find('.carousel__item'), 
      totalItems: 0, 
      dots: $carousel.find('.carousel__dots'), 
      dotLinks: $carousel.find('.carousel__dot-link'), 
      timeout: 0, 
     }, 

     init: function(args) { 
      if ($carousel.length) { 
       s = $.extend({}, this.settings, args); 
       s.totalItems = s.items.length; 

       if (s.totalItems > 1) { 
        this.setupDotsData(); 
        this.activate(); 
        this.eventListeners(); 
        $carousel.addClass('active'); 
       } 
      } 
     }, 

     eventListeners: function() { 
      s.dotLinks.on('click', function() { 
       var index = $(this).data('index'); 
       Carousel.stop(); 
       Carousel.show(index); 
      }); 
     }, 

     setupDotsData: function() { 
      s.dots.each(function() { 
       $(this).find('li').each(function(index) { 
        $(this).data('index', index); 
       }); 
      }); 
     }, 

     activate: function() { 
      if (s.totalItems > 1) { 
       Carousel.show(0); 
      } else { 
       s.items.eq(0).addClass('active'); 
      } 
     }, 

     show: function(index) { 
      s.items.removeClass('active'); 
      s.dotLinks.removeClass('active'); 
      s.items.eq(index).addClass('active'); 
      s.dotLinks.filter(':nth-child(' + (index + 1) + ')').addClass('active'); 
      activeItemIndex = index; 

      Carousel.play(); 
     }, 

     next: function(e) { 
      var nextItemIndex = activeItemIndex + 1 >= s.totalItems ? 0 : activeItemIndex + 1; 
      e && e.preventDefault(); 
      Carousel.stop(); 
      Carousel.show(nextItemIndex); 
     }, 

     play: function() { 
      s.timeout = window.setTimeout(Carousel.next, s.delay); 
     }, 

     stop: function() { 
      window.clearTimeout(s.timeout); 
     } 
    }; 
})(jQuery); 

Carousel.init(); 

Example on JSFIDDLE

+0

我即将离开,所以我无法深入查看代码。但是...我注意到你使用$(“。carousel”)作为整个插件。考虑使用诸如$(“。carousel”)。each(...)之类的东西,然后引用特定.carousel实例中的元素this.find(“。selector_here”)。 –

回答

1

你需要一些修改处理的Carousel多个实例:

https://jsfiddle.net/dmz7wk6f/7/

+0

非常感谢。只是好奇 - 但你能解释一下'$ .proxy()'到底在做什么,它是如何工作的? – Cofey

+1

@Cofey,它有助于传递上下文对象'this',以便在'delay'后执行'next'时,该函数的上下文是单个'carousel'实例,而不是'window'对象。 –

0

试试这个(通过每个转盘循环,抢ID和初始化):

小提琴:https://jsfiddle.net/4o00o7n6/

$(".carousel").each(function(x){ 

    var carouselId = $(this).attr('id'); 
    var Carousel = (function($) { 
     'use strict'; 
     //var wrap = $('.carousel'); 
     var wrap = $('#' + carouselId); 
     var activeItemIndex = 0; 
     var s; 

     return { 
      settings: { 
       delay: 10000, 
       items: wrap.find('.carousel__item'), 
       totalItems: 0, 
       dots: wrap.find('.carousel__dots'), 
       dotLinks: wrap.find('.carousel__dot-link'), 
       timeout: 0, 
      }, 

      init: function(args) { 
       if (wrap && wrap.length) { 
        s = $.extend({}, this.settings, args); 
        s.totalItems = s.items.length; 

        if (s.totalItems > 1) { 
         this.setupDotsData(); 
         this.activate(); 
         this.eventListeners(); 
         wrap.addClass('active'); 
        } 
       } 
      }, 

      eventListeners: function() { 
       s.dotLinks.on('click', function() { 
        var index = $(this).data('index'); 
        Carousel.stop(); 
        Carousel.show(index); 
       }); 
      }, 

      setupDotsData: function() { 
       s.dots.each(function() { 
        $(this).find('li').each(function(index) { 
         $(this).data('index', index); 
        }); 
       }); 
      }, 

      activate: function() { 
       if (s.totalItems > 1) { 
        Carousel.show(0); 
       } else { 
        s.items.eq(0).addClass('active'); 
       } 
      }, 

      show: function(index) { 
       s.items.removeClass('active'); 
       s.dotLinks.removeClass('active'); 
       s.items.eq(index).addClass('active'); 
       s.dotLinks.filter(':nth-child(' + (index + 1) + ')').addClass('active'); 
       activeItemIndex = index; 

       Carousel.play(); 
      }, 

      next: function(e) { 
       var nextItemIndex = activeItemIndex + 1 >= s.totalItems ? 0 : activeItemIndex + 1; 
       e && e.preventDefault(); 
       Carousel.stop(); 
       Carousel.show(nextItemIndex); 
      }, 

      play: function() { 
       s.timeout = window.setTimeout(Carousel.next, s.delay); 
      }, 

      stop: function() { 
       window.clearTimeout(s.timeout); 
      } 
     }; 
    })(jQuery); 

    Carousel.init(); 

}); 
相关问题