Responsive Automatic Carousel With jQuery And CSS Flexbox

Responsive Automatic Carousel With jQuery And CSS Flexbox
File Size: 3.19 KB
Views Total: 1580
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

A minimal, responsive, auto-rotating image carousel with navigation arrows and pagination dots, built with jQuery (not a plugin) and CSS flexbox model.

How to use it:

1. Insert an image list to the carousel container.

<div class="slider-wrapper" id="slider">
  <ul class="slider-img">
    <li>
      <img
        src="https://source.unsplash.com/E0Sbr9d1IV0/960x720"
        alt=""
      />
    </li>
    <li>
      <img
        src="https://source.unsplash.com/zQ1ypq-WHzQ/960x720"
        alt=""
      />
    </li>
    <li>
      <img
        src="https://source.unsplash.com/J-U0WzxOxg8/960x720"
        alt=""
      />
    </li>
    <li>
      <img
        src="https://source.unsplash.com/SKR8jY6r2i8/960x720"
        alt=""
      />
    </li>
    <li>
      <img
        src="https://source.unsplash.com/JpCOGj0uIlI/960x720"
        alt=""
      />
    </li>
    <li>
      <img
        src="https://source.unsplash.com/ybPJ47PMT_M/960x720"
        alt=""
      />
    </li>
  </ul>
</div>

2. The necessary CSS styles for the carousel and its navigation/pagination controls.

/* slider-wrapper */
.slider-wrapper {
  display: flex;
  position: relative;
  width: 100%;
  height: 40vw;
  max-height: 500px;
  min-height: 300px;
  background: #ddd;
  overflow: hidden;
}

.slider-wrapper ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

/* slider-img */
ul.slider-img {
  display: flex;
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  transition: 0.5s;
}

ul.slider-img li {
  flex: 1 0 100%;
}

ul.slider-img li img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* slider-arrow */
ul.slider-arrow {
  position: relative;
  color: #fff;
  font-size: 2rem;
  display: flex;
  justify-content: space-between;
  height: 100%;
  width: 100%;
  text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.6);
}

@media screen and (min-width: 768px) {
  ul.slider-arrow {
    font-size: 2.5rem;
  }
}

ul.slider-arrow li {
  display: flex;
  align-items: center;
  cursor: pointer;
  height: 100%;
  padding: 0 15px;
  opacity: 0.4;
  transition: 0.5s;
}

ul.slider-arrow li:hover {
  opacity: 1;
}

/* slider-dot */
.slider-dot {
  position: absolute;
  bottom: 15px;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  width: 100%;
  color: #fff;
  text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.6);
}

.slider-dot li {
  cursor: pointer;
  margin: 0 8px;
  font-size: 0.6rem;
  opacity: 0.4;
}

.slider-dot li.active {
  opacity: 1;
}

@media screen and (min-width: 768px) {
  .slider-dot li {
    margin: 0 12px;
    font-size: 0.95rem;
  }
}

3. Load the latest jQuery library at the end of the document.

<script src="/path/to/cdn/jquery.min.js"></script>

4. Determine the total amount of images in the carousel.

let sliderCount = $("#slider").find(".slider-img li img").length;

5. Load images into the carousel.

let sliderImg = $("#slider").find(".slider-img");

6. Define the navigation arrows and pagination bullets.

let sliderArrow = `<ul class="slider-arrow"><li class="arrow-left" role="button"><i class="fas fa-chevron-left"></i></li><li class="arrow-right" role="button"><i class="fas fa-chevron-right"></i></li></ul>`;
let sliderDotLi = "";
for (let i = 0; i < sliderCount; i++) {
  sliderDotLi += `<li><i class="fas fa-circle"></i></li>`;
}
let sliderDot = `<ul class="slider-dot">${sliderDotLi}</ul>`;
$("#slider").append(sliderArrow + sliderDot);
let activeDefaultCount = $(".slider-dot li.active").length;
  if (activeDefaultCount != 1) {
    $(".slider-dot li")
      .removeClass()
      .eq(0)
      .addClass("active");
  }
let sliderIndex = $(".slider-dot li.active").index();
sliderImg.css("left", -sliderIndex * 100 + "%");

7. The main JavaScript to switch between images.

function sliderPos() {
  sliderImg.css("left", -sliderIndex * 100 + "%");
  $(".slider-dot li")
    .removeClass()
    .eq(sliderIndex)
    .addClass("active");
}

$(".arrow-right").click(function() {
  sliderIndex >= sliderCount - 1 ? (sliderIndex = 0) : sliderIndex++;
  sliderPos();
});

$(".arrow-left").click(function() {
  sliderIndex <= 0 ? (sliderIndex = sliderCount - 1) : sliderIndex--;
  sliderPos();
});

$(".slider-dot li").click(function() {
  sliderIndex = $(this).index();
  sliderPos();
});

let goSlider = setInterval(() => {
  $(".arrow-right").click();
}, 3000);

$("#slider").on({
  mouseenter: () => {
    clearInterval(goSlider);
  },
  mouseleave: () => {
    goSlider = setInterval(() => {
      $(".arrow-right").click();
    }, 3000);
  }
});

This awesome jQuery plugin is developed by huangaho423. For more Advanced Usages, please check the demo page or visit the official website.