在单个页面上使用多个(猫头鹰)轮播 [英] Using multiple (owl) carousel on a single page

查看:95
本文介绍了在单个页面上使用多个(猫头鹰)轮播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Google上寻找在单个页面上使用多个轮播的方法,但找不到适合我的任何解决方案.谁能帮忙吗?

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.

这是代码:

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

I have named the carousel as as owl-demo and 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

推荐答案

更新后的代码应如下所示: https: //jsfiddle.net/wtg76spd/1/

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

JavaScript:

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(仅更改第一行):

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)使用.each()代替复制代码.

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

2)最好使用class而不是#owl-demo和#owl-demo-1-假设您没有2个而是100个滑块.您还会给他们ID吗?但是我没有在示例中进行更改.

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)我对下一个/上一个按钮使用了close()和find()方法.这样,我有2个回调函数,而不是4个.

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

这篇关于在单个页面上使用多个(猫头鹰)轮播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆