横向滚动的图像库

创建一个可以横向滚动的图像库。

  • 使用 position: absolute.thumbnails 定位在容器底部。
  • 使用 scroll-snap-type: x mandatoryoverscroll-behavior-x: contain 创建水平滚动的吸附效果。使用 scroll-snap-align: start 将元素吸附到容器的起始位置。
  • 使用 scrollbar-width: none 隐藏滚动条,并使用伪元素 ::-webkit-scrollbar 的样式 display: none
  • 使用 Element.scrollTo() 定义一个 scrollToElement 函数,用于将图像库滚动到指定的项。
  • 使用 Array.prototype.map()Array.prototype.join() 填充 .thumbnails 元素。给每个缩略图添加一个 data-id 属性,值为图像的索引。
  • 使用 Document.querySelectorAll() 获取所有缩略图元素。使用 Array.prototype.forEach() 在每个缩略图上注册 'click' 事件的处理程序,使用 EventTarget.addEventListener()scrollToElement 函数。
  • 使用 Document.querySelector()EventTarget.addEventListener() 注册一个 'scroll' 事件的处理程序。使用 highlightThumbnail 函数更新 .thumbnails 元素以匹配当前的滚动位置。
<div class="gallery-container">
  <div class="thumbnails"></div>
  <div class="slides">
    <div><img src="https://picsum.photos/id/1067/540/720"></div>
    <div><img src="https://picsum.photos/id/122/540/720"></div>
    <div><img src="https://picsum.photos/id/188/540/720"></div>
    <div><img src="https://picsum.photos/id/249/540/720"></div>
    <div><img src="https://picsum.photos/id/257/540/720"></div>
    <div><img src="https://picsum.photos/id/259/540/720"></div>
    <div><img src="https://picsum.photos/id/283/540/720"></div>
    <div><img src="https://picsum.photos/id/288/540/720"></div>
    <div><img src="https://picsum.photos/id/299/540/720"></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;
}

```css
.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();