I'm currently using Owl Carousel to show a gallery on desktop/laptop sized devices. However on smaller devices I'd like to disable the plugin and show each image stacked underneath one another.
Is this possible? I'm aware you can tweak Owl Carousel to show a certain number of images on screen at certain breakpoints. But I would like to be able to turn it off completely, removing all the divs etc.
Here's a pen of what i'm currently working with at the moment: http://codepen.io/abbasinho/pen/razXdN
HTML:
<div id="carousel">
<div class="carousel-item" style="background-image: url(http://owlgraphic.com/owlcarousel/demos/assets/owl1.jpg);">
</div>
<div class="carousel-item" style="background-image: url(http://owlgraphic.com/owlcarousel/demos/assets/owl2.jpg);">
</div>
<div class="carousel-item" style="background-image: url(http://owlgraphic.com/owlcarousel/demos/assets/owl3.jpg);">
</div>
</div>
CSS:
#carousel {
width: 100%;
height: 500px;
}
.carousel-item {
width: 100%;
height: 500px;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
jQuery:
$("#carousel").owlCarousel({
navigation : true,
slideSpeed : 300,
paginationSpeed : 400,
singleItem: true
});
Any help is gratefully received as ever!
Simple, use jquery. Add a class to the carousel's container div, for example "
<div id="carousel class='is_hidden'>
" with some css for example ".is_hidden{display:none;}
". Then use jquery to remove the class, or add the class, depending on window width.I had to disable plugin if the screen width is bigger than 854px. Sure, you can change the code to fit your needs. Here is my solution:
3.1. If width is good to call the plugin AND the plugin hasn't been called yet or it was destroyed earlier (I use the
'off'
class to know it), THEN call the plugin.3.2. if width isn't good to call the plugin AND the plugin is active now, THEN destroy it with Owl's trigger event
destroy.owl.carousel
and remove the unnecessary wrapper element'owl-stage-outer'
after it.And a bit of CSS to show disabled Owl element:
Easier to use a CSS based solution
mcmimik's answer is correct, but I had to make one change for it to work for me.
In the function:
Swap out
owl.addClass('off').trigger('destroy.owl.carousel');
forowl.addClass('off').data("owlCarousel").destroy();
:Working Fiddle: https://jsfiddle.net/j6qk4pq8/187/