Owl Carousal2 with items 1 and loop true
Asked Answered
C

7

7

$(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.

Coastland answered 26/5, 2015 at 4:25 Comment(0)
S
17

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;
        }
    }
  })
});
Schroth answered 27/5, 2015 at 17:39 Comment(2)
you are right if item is one than loop false, but scenario is that item one and loop true is possible?Coastland
It should be possible, but it is a bug in the owl carousel.Schroth
K
13

Try this:

$('.owl-carousel').owlCarousel({
    loop: $('.owl-carousel .item').size() > 1 ? true:false,
    items: 1,
    margin:10,
    nav:true
})
Koph answered 25/11, 2015 at 14:38 Comment(0)
N
2

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.

Nephograph answered 27/4, 2016 at 23:4 Comment(0)
F
1
var a = $(".owl-parent");
loop: owl.children().length > 1

Change the selector according to your needs.

This works too.

Fossiliferous answered 23/10, 2018 at 9:13 Comment(0)
A
0

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

Avina answered 26/5, 2015 at 4:55 Comment(1)
that is not an issue.Coastland
R
0

check this demo

is this the one you are looking for ?

$('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    nav:true,
    responsive:{
        0:{
            items:1
        },
        600:{
            items:3
        },
        1000:{
            items:5
        }
    }
})
Rheology answered 27/5, 2015 at 3:31 Comment(1)
Hello @Ashley u r right but at responsive but i want items:1 only and also <div class="item"><h4>1</h4></div> only one. and loop property true. i know this type of situation does not occurs but in case.Coastland
C
0
   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: "",         
        });
    }
Coastland answered 15/9, 2015 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.