Owl Carousal2 with items 1 and loop true

2019-04-29 12:42发布

$(document).ready(function(){
  $('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    nav:true,
    items: 1
  })
});
<div class="owl-carousel">
    <div class="item"><h4>1</h4></div>
</div>
<link href="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/owl.carousel.js"></script>

Console Error : TypeError: items[clones[(clones.length - 1)]] is undefined. this error due to only one item div and property loop true and item 1. So. any solution at this situation. I know this type of Situation does not occurs but if any solutions please tell me Thanks a lot.

7条回答
贼婆χ
2楼-- · 2019-04-29 13:12
var a = $(".owl-parent");
loop: owl.children().length > 1

Change the selector according to your needs.

This works too.

查看更多
老娘就宠你
3楼-- · 2019-04-29 13:20

I made a very basic fix (on the owl.carousel.js file). Check my comment here https://github.com/OwlCarousel2/OwlCarousel2/issues/1200#issuecomment-215254526

It's an extremely quick & dirty fix. That I'll try to enhance as soon as I can.

查看更多
孤傲高冷的网名
4楼-- · 2019-04-29 13:22
   if($(".owl-carousel").length > 0){
        $(".owl-carousel").owlCarousel({
            items: 1,       
            nav: $(".owl-carousel > .item").length <= 1 ? false : true,
            dots: false,
            loop:$(".owl-carousel > .item").length <= 1 ? false : true,
            autoplay:true,
            navText: "",         
        });
    }
查看更多
我命由我不由天
5楼-- · 2019-04-29 13:33

Add onInitialize and check how many items the carousel contains. If the carousel has 1 or less items, set loop to false.

$(document).ready(function(){
  $('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    nav:true,
    items: 1,
    onInitialize: function (event) {
        if ($('.owl-carousel .item').length <= 1) {
           this.settings.loop = false;
        }
    }
  })
});
查看更多
时光不老,我们不散
6楼-- · 2019-04-29 13:35

Include owl.carousel.min.css file below the jquery.min.js file and also include those files at the bottom of the page.

查看更多
【Aperson】
7楼-- · 2019-04-29 13:37

Try this:

$('.owl-carousel').owlCarousel({
    loop: $('.owl-carousel .item').size() > 1 ? true:false,
    items: 1,
    margin:10,
    nav:true
})
查看更多
登录 后发表回答