javascript - Owl Carousel with conditionals on its options -
i've got dozen galleries , of them has 1 image. causing carousel , page break because carousel has loop = true
set on it. i'm trying write condition if there's more 1 item in carousel, make loop = true
else make loop = false
. however, took shot @ doesn't seem working.
$(".mbgc-gallery").owlcarousel({ margin: 0, dots: false, nav:($(".mbgc-gallery .owl-item").length > 1) ? true: false, navtext: [], loop:($(".mbgc-gallery .owl-item").length > 1) ? true: false, autoplay: false, autoplayhoverpause: true, responsive: { 0: { items: 1 }, 600: { items: 1 }, 1000: { items: 1 } } });
can work or need after initializes?
edit forgot mention page have more 1 gallery on it, not sure if needs wrapped in .each
function or maybe unique selector? have data attributes set on each gallery has unique id.
your query in owlcarousel configuration scans whole document again. select .mbgc-gallery
s , select .owl-item
s of .mbgc-gallery
s.
want test "this" carousel. should work:
$(".mbgc-gallery").each(function(index) { var $gallery = $(this); var onmultiple = $gallery.children(".owl-item").length > 1 ? true : false; $gallery.owlcarousel({ loop: onmultiple, [...] }); };
edit:
made snippet.
this?
$('.owl-carousel').each(function(i) { var ifmultiple = false; $thisgallery = $(this); if($thisgallery.children('.item').length > 1) { ifmultiple = true; } $thisgallery.owlcarousel({ loop: ifmultiple, autoplay: true, dots: true, nav: true, items: 1, autowidth: true, nav: false, dots: false, responsive:false }) })
.item { box-sizing: border-box; padding: 2rem; width: 200px; height: 150px; background: yellow; } .owl-carousel { border: 1px solid black; width: 200px !important; }
<link href="https://owlcarousel2.github.io/owlcarousel2/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet"/> <link href="https://owlcarousel2.github.io/owlcarousel2/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://github.com/owlcarousel2/owlcarousel2/raw/develop/dist/owl.carousel.min.js"></script> <h2>multiple</h2> <div class="owl-carousel"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> </div> <h2>single</h2> <div class="owl-carousel"> <div class="item">1</div> </div> <h2>multiple</h2> <div class="owl-carousel"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> <h2>single</h2> <div class="owl-carousel"> <div class="item">1</div> </div>
Comments
Post a Comment