Reputation: 17
I want to set preloader image in owl carousel2. I have read the docs but i didn't find any information to use preloader image in lazyload plugin option.
here is the plugin website :
https://owlcarousel2.github.io/OwlCarousel2/
My Code :
<div class="single-product owl-carousel owl-theme">
<a href="https://source.unsplash.com/0utRJ1mZoZg/1080x1350" data-fancybox="images">
<img data-src="https://source.unsplash.com/0utRJ1mZoZg/600x600" class="img-fluid owl-lazy" />
</a>
<a href="https://source.unsplash.com/aiNU4cA4UzQ/1080x1350" data-fancybox="images">
<img data-src="https://source.unsplash.com/aiNU4cA4UzQ/600x600" class="img-fluid owl-lazy" />
</a>
<a href="https://source.unsplash.com/295NLwGdrKM/1080x1350" data-fancybox="images">
<img data-src="https://source.unsplash.com/295NLwGdrKM/600x600" class="img-fluid owl-lazy" />
</a>
<a href="https://source.unsplash.com/NzsTFFB6Ng8/1080x1350" data-fancybox="images">
<img data-src="https://source.unsplash.com/NzsTFFB6Ng8/600x600" class="img-fluid owl-lazy" />
</a>
</div>
$('.single-product').owlCarousel({
loop : true,
items : 1,
nav : false,
lazyLoad: true,
});
I need to give preloader gif image, for better UX.
Could you please help me? Thanks a lot
Upvotes: 1
Views: 11027
Reputation: 2064
You can set a background image of a loader gif. Also, I've wrapped the OwlCarousel call inside $(window).load()
so that it'll wait for the images to load before it's initiated while the loader will take care of any Flash Of Unstyled Content (FOUC)
$(window).on('load', function() {
$('.single-product').owlCarousel({
autoHeight: true,
lazyLoad: true,
items: 1,
autoplay: true,
nav: false,
dots: true,
loop: true,
});
});
.owl-carousel {
height: 300px;
}
.owl-carousel {
background: url("https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/ajax-loader.gif") no-repeat center center;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.min.css">
<div class="single-product owl-carousel owl-theme">
<a href="https://source.unsplash.com/0utRJ1mZoZg/1080x1350" data-fancybox="images">
<img data-src="https://source.unsplash.com/0utRJ1mZoZg/600x600" class="img-fluid owl-lazy" />
</a>
<a href="https://source.unsplash.com/aiNU4cA4UzQ/1080x1350" data-fancybox="images">
<img data-src="https://source.unsplash.com/aiNU4cA4UzQ/600x600" class="img-fluid owl-lazy" />
</a>
<a href="https://source.unsplash.com/295NLwGdrKM/1080x1350" data-fancybox="images">
<img data-src="https://source.unsplash.com/295NLwGdrKM/600x600" class="img-fluid owl-lazy" />
</a>
<a href="https://source.unsplash.com/NzsTFFB6Ng8/1080x1350" data-fancybox="images">
<img data-src="https://source.unsplash.com/NzsTFFB6Ng8/600x600" class="img-fluid owl-lazy" />
</a>
</div>
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
Upvotes: 5