JavaScript Image Carousel with Thumbnails

JavaScript Image Carousel with Thumbnails
Code Snippet:Photo Gallery
Author: dave deCay
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 1,889
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code creates a responsive image carousel with thumbnail navigation. It lets you view different images by clicking on thumbnails. The core function, “changeImage,” dynamically swaps the main image and highlights the active thumbnail for seamless image browsing. Perfect for showcasing multiple images in an easy-to-use and user-friendly way.

How to Create Image Carousel With Thumbnails in JavaScript

1. First, create the HTML structure for the image carousel and thumbnails. Place the following code inside the <body> tag of your HTML document. Replace sample images with the URL of your own images and add more thumbnail images inside the .photo-gallery__wrapper div as needed.

<section class="wrapper">
  <div class="image-holder">
    <img src="https://images.unsplash.com/photo-1544568100-847a948585b9?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=800&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ" \>
  </div>

  <div class="photo-gallery__wrapper">
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1544568100-847a948585b9?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);" class="active">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1517423440428-a5a00ad493e8?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1510771463146-e89e6e86560e?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1507146426996-ef05306b995a?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1530281700549-e82e7bf110d6?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1548199973-03cce0bbc87b?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1552053831-71594a27632d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1518717758536-85ae29035b6d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1535930891776-0c2dfb7fda1a?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
    <div class="image-thumbnail">
      <img src='https://images.unsplash.com/photo-1504595403659-9088ce801e29?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ' alt='' onclick="changeImage(this);">
    </div>
  </div>
</section>

2. Now, style the carousel and thumbnails using CSS. Add the following CSS code inside a <style> tag or an external CSS file. You can customize the styles according to your design preferences.

section.wrapper {
  width: 100%;
}

.photo-gallery__wrapper {
  width: 800px;
  margin: 0 auto;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
}
.photo-gallery__wrapper div {
  height: 80px;
  overflow: hidden;
}

.image-holder {
  width: 800px;
  height: 400px;
  overflow: hidden;
  margin: 0 auto;
}
.image-holder img {
  width: 800px;
  height: 400px;
  object-fit: cover;
  object-position: 50% 50%;
}
.image-thumbnail img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  transition: transform 0.35s;
  transform: scale(1);
  filter: grayscale(1);
}
.image-thumbnail img.active {
  filter: none;
}
.image-thumbnail img:hover {
  transition: transform 0.35s, filter 0.35s linear;
  transform: scale(1.12);
  filter: none;
}

3. The core functionality is implemented with the following JavaScript code. This code swaps the main image and highlights the active thumbnail when a thumbnail is clicked. Add the JavaScript code to your document within a <script> tag or link to an external JavaScript file.

let changeImage = (e) => {

    let imgSrc = e.src;
    imgSrc = imgSrc.replace(/400/g, '800');
    document.querySelector('.image-holder > img').src = imgSrc;
    document.querySelector('.image-thumbnail > img.active').classList.toggle('active');
    e.classList.toggle('active');

};

That’s all! hopefully, you have successfully created a JavaScript Image Carousel with Thumbnails on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X