A scrolling horizontal image gallery is method of display your images and media. In this blog post, we will guide you through the process of creating a simple and quick JavaScript, CSS and html scrolling horizontal image gallery .With just a few lines of code, you’ll be able to transform your static image collection into a horizontal carousel that your visitors will find much easier to view.

Tags: Horizontal Image Gallery, Image Gallery, Image Carousel, Simple Image Gallery, quick image gallery, css image gallery, javascript image gallery, css image gallery,

 

<div class="gallery-container">
  <div class="thumbnails"></div>
  <div class="slides">
    <div><img src="https://codesnippetsandtutorials.com/2023/08/30/a-collection-of-git-tutorials-courses-and-ebooks-covering-git-branching-git-lfs-beginner-git-tutorials-and-more/"></div>
    <div><img src="https://codesnippetsandtutorials.com/2023/08/30/a-collection-of-git-tutorials-courses-and-ebooks-covering-git-branching-git-lfs-beginner-git-tutorials-and-more/"></div>
    <div><img src="https://codesnippetsandtutorials.com/2023/08/30/a-collection-of-git-tutorials-courses-and-ebooks-covering-git-branching-git-lfs-beginner-git-tutorials-and-more/"></div>
  </div>
</div>

.gallery-container {
  position: relative;
  display: flex;
  justify-content: center;
}

.thumbnails {
  position: absolute;
  bottom: 8px;
  display: flex;
  flex-direction: row;
  gap: 6px;
}

.thumbnails div {
  width: 8px;
  height: 8px;
  cursor: pointer;
  background: #aaa;
  border-radius: 100%;
}

.thumbnails div.highlighted {
  background-color: #777;
}

.slides {
  margin: 0 16px;
  display: grid;
  grid-auto-flow: column;
  gap: 1rem;
  width: 540px;
  padding: 0 0.25rem;
  height: 720px;
  overflow-y: auto;
  overscroll-behavior-x: contain;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
}

.slides > div {
  scroll-snap-align: start;
}

.slides img {
  width: 540px;
  object-fit: contain;
}

.slides::-webkit-scrollbar {
  display: none;
}
const slideGallery = document.querySelector('.slides');
const slides = slideGallery.querySelectorAll('div');
const thumbnailContainer = document.querySelector('.thumbnails');
const slideCount = slides.length;
const slideWidth = 540;

const highlightThumbnail = () => {
  thumbnailContainer
    .querySelectorAll('div.highlighted')
    .forEach(el => el.classList.remove('highlighted'));
  const index = Math.floor(slideGallery.scrollLeft / slideWidth);
  thumbnailContainer
    .querySelector(`div[data-id="${index}"]`)
    .classList.add('highlighted');
};

const scrollToElement = el => {
  const index = parseInt(el.dataset.id, 10);
  slideGallery.scrollTo(index * slideWidth, 0);
};

thumbnailContainer.innerHTML += [...slides]
  .map((slide, i) => `<div data-id="${i}"></div>`)
  .join('');

thumbnailContainer.querySelectorAll('div').forEach(el => {
  el.addEventListener('click', () => scrollToElement(el));
});

slideGallery.addEventListener('scroll', e => highlightThumbnail());

highlightThumbnail();
CC BY 4.0 no changes made - 30 Seconds of Code