owl carousel 2 not work with loop and 1 items (Bug Fixed Now)

后端 未结 4 1044
清歌不尽
清歌不尽 2021-02-15 19:19

I work with owl carousel 2 for carousel content.

JS:

$(\'#owl-demo\').owlCarousel({
    loop: true,
    margin: 10,
    nav: true,
    items: 1,
});


        
相关标签:
4条回答
  • 2021-02-15 19:53

    I hope the below method will solve your problem.
    You dont need to edit the owl carousel js. The same method can be applied to Bx Slider also.

    $('.owl-demo').owlCarousel({
        margin: 0,
        responsiveClass: true,
        smartSpeed: 500,
        dots: ($(".owl-carousel .item").length > 1) ? true: false,
        loop:($(".owl-carousel .item").length > 1) ? true: false,
        responsive: {
            0: {
                items: 1,
            },
            500: {
                items: 1,
            },
            768: {
                items: 1,
            }
        } 
    })
    
    0 讨论(0)
  • 2021-02-15 19:59

    I report this bug to Owl developer group and fix this problem here.

    Change this line in Commit

    view = Math.max(settings.items * 2, 2),
    

    Now this worked in demo

    0 讨论(0)
  • 2021-02-15 20:00

    I used this method to solve the problem and I think it's pretty easy.

    var options={
        margin: 10,
        nav: true,
        items: 1
        };
       if($('#owl-demo .owl-item').length>1){
           options.loop=true;
       }
       $('#owl-demo').owlCarousel(options);
    
    0 讨论(0)
  • 2021-02-15 20:01

    You could check the number of .item elements before you call your plugin like so:

    // Be more specific with your selector if .items is used elsewhere on the page. 
    var items = $('.items');
    if(items.length > 1) {
        $('#owl-demo').owlCarousel({
            loop: true,
            ...
        });
    } else {
        // same as above but with loop: false;
    }
    
    0 讨论(0)
提交回复
热议问题