Owl Carousel 2 - autoHeight (multiple items)
At the moment the Owl Carousel autoHeight works only with 1 item on screen. Their plan is to calculate all visible items and change height according to highest item.
I work around this problem by calling the .active classes on the visible items, and give the invisible items a small height. Is there already a more elegant solution?
HTML
<script src="https://rawgit.com/OwlFonk/OwlCarousel2/develop/src/js/owl.carousel.js"></script>
<link rel="stylesheet" href="https://cdn.rawgit.com/OwlFonk/OwlCarousel2/develop/src/css/owl.theme.default.css">
<link rel="stylesheet" href="https://rawgit.com/OwlFonk/OwlCarousel2/develop/src/css/owl.carousel.css">
<div class="owl-carousel">
<div class="item" style="height:100px;">
1
</div>
<div class="item" style="height:150px;">
2
</div>
<div class="item" style="height:200px;">
3
</div>
<div class="item" style="height:250px;">
4
</div>
<div class="item" style="height:300px;">
5
</div>
<div class="item" style="height:350px;">
6
</div>
<div class="item" style="height:400px;">
7
</div>
<div class="item" style="height:450px;">
8
</div>
</div>
<a class="prev">prev</a>
<a class="next">next</a>
CSS
.owl-carousel .owl-item {
border:1px red solid;
background:#CCC;
box-sizing: border-box;
-o-box-sizing: border-box;
-ms-box-sizing: border-box;
-moz-box-sizing: border-box;
-khtml-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
.owl-carousel,
.owl-stage-outer { transition: height 500ms ease-in-out 0s; }
JavaScript
$('.owl-carousel').owlCarousel({
loop: true,
items: 3,
margin: 1,
autoHeight: true,
onInitialized: setOwlStageHeight,
onResized: setOwlStageHeight,
onTranslated: setOwlStageHeight
});
function setOwlStageHeight(event) {
var maxHeight = 0;
$('.owl-item.active').each(function () {
var thisHeight = parseInt( $(this).height() );
maxHeight=(maxHeight>=thisHeight?maxHeight:thisHeight);
});
$('.owl-carousel').css('height', maxHeight );
$('.owl-stage-outer').css('height', maxHeight ); // TO CORRECT DRAG-AREA
};
owl = $('.owl-carousel').owlCarousel();
$(".prev").click(function () {
owl.trigger('prev.owl.carousel');
});
$(".next").click(function () {
owl.trigger('next.owl.carousel');
});