Skip to content

Instantly share code, notes, and snippets.

@tkfm-yamaguchi
Last active January 23, 2019 15:47
Show Gist options
  • Save tkfm-yamaguchi/a3b53e43118cef3c3e22d57e82130332 to your computer and use it in GitHub Desktop.
Save tkfm-yamaguchi/a3b53e43118cef3c3e22d57e82130332 to your computer and use it in GitHub Desktop.
OwlCarousel's bug when {loop: false, autoWidth: true} (reproduction & workaround)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Workaround for OwlCarousel's autoWidth:true/loop:false bug</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.min.css">
<style>
/* common style */
.carousel-item {
width: 150px; /* item width */
}
.container {
margin: 20px auto;
}
/* Workarounds */
.workaround.container {
/* Workaround: instead of owl-stage-outer, container hides the overflowed items */
overflow-x: hidden;
/* centering carousel */
margin-left: auto;
margin-right: auto;
}
.workaround .owl-carousel {
/* Workaround: this should be up to 2x of minimum width of the item */
width: 300px;
/* centering carousel */
margin-left: auto;
margin-right: auto;
}
.workaround .owl-carousel .owl-stage-outer {
/* Workaround: stop stage-outer to hide items overflow (default 'hidden') */
overflow: visible;
}
</style>
</head>
<body>
<h3>Bug Reproduction</h3>
<div class="container">
<div class="owl-carousel owl-theme">
<!-- {{{ -->
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 1 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 2 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 3 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 4 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 5 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 6 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 7 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 8 </div></div>
<!-- }}} -->
</div>
</div>
<h3>Workaround applied</h3>
<div class="workaround container">
<div class="owl-carousel owl-theme">
<!-- {{{ -->
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 1 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 2 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 3 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 4 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 5 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 6 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 7 </div></div>
<div class="carousel-item"><img src="https://via.placeholder.com/150" /><div class="carousel-item-title"> Item 8 </div></div>
<!-- }}} -->
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
<script>
$(function(){
$('.owl-carousel').owlCarousel({
loop: false,
autoWidth: true,
center: true,
margin: 10,
nav: true,
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment