Using multiple (owl) carousel on a single page

后端 未结 1 528
误落风尘
误落风尘 2020-12-20 09:26

I have been looking at ways on google to use multiple carousel on a single page and yet did not find any of the solutions working for me. Can anyone of you please help.

相关标签:
1条回答
  • 2020-12-20 10:04

    Updated code should look like this: https://jsfiddle.net/wtg76spd/1/

    JavaScript:

    $(document).ready(function() {
    
      $("#owl-demo, #owl-demo-1").each(function() {
        $(this).owlCarousel({
          items : 6, //10 items above 1000px browser width
          itemsDesktop : [1000,6], //5 items between 1000px and 901px
          itemsDesktopSmall : [900,3], // 3 items betweem 900px and 601px
          itemsTablet: [600,2], //2 items between 600 and 0;
          itemsMobile : false // itemsMobile disabled - inherit from itemsTablet option
        });
      });
      // Custom Navigation Events
      $(".next").click(function(){$(this).closest('.span12').find('.owl-carousel').trigger('owl.next');})
      $(".prev").click(function(){$(this).closest('.span12').find('.owl-carousel').trigger('owl.prev');})
    });
    

    CSS (just first line changed):

    //before
     #owl-demo .item{
    //after
     #owl-demo .item, #owl-demo-1 .item{
    //class "owl-demo" would do better in this case
    

    1) Use .each() instead of copying code.

    2) It'd be better to use class instead of #owl-demo and #owl-demo-1 - let's say you had not 2 but 100 sliders. Would you still give them IDs? However I didn't change it in example.

    3) I used closest() and find() methods for next/prev buttons. This way I have 2 callback functions instead of 4.

    0 讨论(0)
提交回复
热议问题