Menu Close

Image Lazy loading Using Slick Slider

Image lazy loading

For image lazy loading, we will change the data-lazy attribute instead of the src attribute in the image tag. Follow the below snippet to learn lazy loading.

Follow the complete code with an example.

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.5.9/slick.min.css"/>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.5.9/slick-theme.min.css"/>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    <title>Image Lazy Loading</title>
</head>
<body>
<div class="main">
  <div class="slider slide-show">
    <div class="img-wish">
        <img data-lazy="s1.png" >
        </div>
    <div class="img-wish">
        <img data-lazy="s2.png" >
        </div>
    <div class="img-wish">
        <img data-lazy="s3.png" >
        </div>
    <div class="img-wish">
        <img data-lazy="s4.png" >
        </div>
    <div class="img-wish">
        <img data-lazy="s5.png" >
        </div>
    <div class="img-wish">
        <img data-lazy="s6.png" >
        </div>
  </div>
</div>
</body>  
    <script>
          $('.slide-show').slick({ 
              lazyLoad: 'ondemand',
              slidesToShow: 3,
              slidesToScroll: 1
            });
    </script>
</html>
 

CSS:

Create external stylesheet and add it to the above example

body{
    background:#ccc;
}
.main {
    width: 800px;
    display:block;
    margin:0 auto;
}
.img-wish {
    text-align: center !important;
    margin: 20px;
}
.img-wish img {
    width: 100%;
} 

More examples for slick slider

Posted in HTML, jQuery

You can also read...