在单个页面上使用多个(猫头鹰)轮播

时间:2016-09-25 10:31:37

标签: javascript jquery carousel owl-carousel

我一直在寻找谷歌在一个页面上使用多个轮播的方式,但却找不到任何适用于我的解决方案。你们中的任何人都可以帮忙。

以下是代码:

HTML

<!-- Carousel 1 -->
<div id="demo">
    <div class="container">
        <div class="row">
            <div class="span12">
                <div id="owl-demo" class="owl-carousel">
                    <div class="item">
                        <h1>1</h1>
                    </div>
                    <div class="item">
                        <h1>2</h1>
                    </div>
                    <div class="item">
                        <h1>3</h1>
                    </div>
                    <div class="item">
                        <h1>4</h1>
                    </div>
                    <div class="item">
                        <h1>5</h1>
                    </div>
                    <div class="item">
                        <h1>6</h1>
                    </div>
                    <div class="item">
                        <h1>7</h1>
                    </div>
                    <div class="item">
                        <h1>8</h1>
                    </div>
                    <div class="item">
                        <h1>9</h1>
                    </div>
                    <div class="item">
                        <h1>10</h1>
                    </div>
                </div>
                <div class="customNavigation"> <a class="btn prev">Previous</a> <a class="btn next">Next</a> </div>
            </div>
        </div>
    </div>
</div>
<!-- Carousel 2 -->
<div id="demo1">
    <div class="container">
        <div class="row">
            <div class="span12">
                <div id="owl-demo-1" class="owl-carousel">
                    <div class="item">
                        <h1>1</h1>
                    </div>
                    <div class="item">
                        <h1>2</h1>
                    </div>
                    <div class="item">
                        <h1>3</h1>
                    </div>
                    <div class="item">
                        <h1>4</h1>
                    </div>
                    <div class="item">
                        <h1>5</h1>
                    </div>
                    <div class="item">
                        <h1>6</h1>
                    </div>
                    <div class="item">
                        <h1>7</h1>
                    </div>
                    <div class="item">
                        <h1>8</h1>
                    </div>
                    <div class="item">
                        <h1>9</h1>
                    </div>
                    <div class="item">
                        <h1>10</h1>
                    </div>
                </div>
                <div class="customNavigation"> <a class="btn prev">Previous</a> <a class="btn next">Next</a> </div>
            </div>
        </div>
    </div>
</div>

我已将旋转木马命名为owl-demo和owl-demo-1

的Javascript

$(document).ready(function() {

  var owl = $("#owl-demo");
  owl.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(){owl.trigger('owl.next');})
  $(".prev").click(function(){owl.trigger('owl.prev');})


});

JsFiddle

1 个答案:

答案 0 :(得分:2)

更新的代码应如下所示: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(只改变了第一行):

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

1)使用.each()而不是复制代码。

2)最好使用class而不是#owl-demo和#owl-demo-1 - 假设你没有2个而是100个滑块。你还会给他们身份证吗?但是我没有在示例中更改它。

3)我对next / prev按钮使用了nearest()和find()方法。这样我就有2个回调函数而不是4个。