Menu Close

Slick Slider Carousel Add and Remove

Slick slider add remove

Let’s see how to create a slick slider image carousel with add and remove slides using html, css and jquery. Follow the below snippet to create add and remove slide carousel with responsive design.

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>Slick Slider Add and Remove Slides</title>
</head>
<body>
    <div class="main">
        <div class="slider slide-show">
            <div><h2>1</h2></div>
            <div><h2>2</h2></div>
            <div><h2>3</h2></div>
            <div><h2>4</h2></div>
            <div><h2>5</h2></div>
            <div><h2>6</h2></div>
        </div>
        <div class="button-style">
            <a class="button-slide add-slide">Add</a>
            <a class="button-slide remove-slide">Remove</a>
        </div>
</div>
    </body>
    <script>
    $('.slide-show').slick({
        infinite: false,
        speed: 200,
        slidesToShow: 3,
        slidesToScroll: 1
    });
var slideIndex = 6;
$('.add-slide').on('click', function() {
  slideIndex++;
  $('.slide-show').slick('slickAdd','<div><h2>' + slideIndex + '</h2></div>');
});

$('.remove-slide').on('click', function() {
  $('.slide-show').slick('slickRemove',slideIndex - 1);
  if (slideIndex !== 0){
    slideIndex--;
  }
});
    </script> 
</html> 

CSS:

Create an external stylesheet and add it to the above example to view the slides with the responsive design.

body{
  background:#ccc;
}
.main {
  font-family:Arial;
  width:500px;
  display:block;
  margin:0 auto;
}
h2 {
    background: #FFD700;
    color: #0000CD;
    font-size: 36px;
    line-height: 100px;
    margin: 10px;
    padding: 2%;
    position: relative;
    text-align: center;
}
.button-style {
    padding: 0;
    margin-bottom: 10px;
}
.button-style .button-slide {
    background: #FFD700;
    color: #0000CD;
    margin: 5px;
    width:200px;
    float:left;
}
.button-slide {
    background: #3498db;
    color: #FFD700;
    display: block;
    font-size: 16px;
    margin: 20px auto;
    padding: 20px;
    text-align: center;
    text-decoration: none;
    width: 50%;
} 

More examples for slick slider

Posted in HTML, JavaScript, jQuery

You can also read...