You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现中间聚焦、两侧延伸的可切换图片滑块?

中心聚焦式图片轮播实现方案

这个组件叫中心聚焦式图片轮播(Center-Focused Image Carousel),以下是更规范、可控的实现方式,比单纯用inline-flex调整尺寸更完善:

核心实现思路

以flex布局为基础容器,通过动态调整元素的缩放比例、层级和位置实现中心聚焦效果,配合JS控制切换逻辑,支持任意数量图片的循环轮播。

1. HTML结构

不用固定写死5个div,用容器包裹所有轮播项,搭配左右切换按钮:

<div class="carousel-container">
  <button class="carousel-btn prev-btn">&lt;</button>
  <div class="carousel-track">
    <div class="carousel-item"><img src="img1.jpg" alt="展示图1"></div>
    <div class="carousel-item"><img src="img2.jpg" alt="展示图2"></div>
    <div class="carousel-item"><img src="img3.jpg" alt="展示图3"></div>
    <div class="carousel-item"><img src="img4.jpg" alt="展示图4"></div>
    <div class="carousel-item"><img src="img5.jpg" alt="展示图5"></div>
  </div>
  <button class="carousel-btn next-btn">&gt;</button>
</div>

2. CSS样式

用flex让轮播项横向排列,通过transform: scale()和z-index区分中心项与两侧项,给容器加overflow: hidden避免出现横向滚动条:

.carousel-container {
  position: relative;
  width: 80%;
  margin: 0 auto;
  overflow: hidden;
}

.carousel-track {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  transition: transform 0.3s ease;
}

.carousel-item {
  flex-shrink: 0;
  width: 150px;
  height: 100px;
  transition: all 0.3s ease;
  z-index: 1;
}

.carousel-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

/* 中心聚焦项样式 */
.carousel-item.active {
  transform: scale(1.5);
  z-index: 10;
  width: 200px;
  height: 150px;
}

/* 两侧相邻项样式 */
.carousel-item.prev, .carousel-item.next {
  transform: scale(1.2);
  z-index: 5;
}

.carousel-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  border: none;
  background: rgba(0,0,0,0.5);
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
  z-index: 20;
}

.prev-btn { left: 10px; }
.next-btn { right: 10px; }

3. JavaScript逻辑

通过类名切换控制轮播状态,实现循环切换(边界项切换时自动跳转到另一端):

const track = document.querySelector('.carousel-track');
const items = Array.from(document.querySelectorAll('.carousel-item'));
const prevBtn = document.querySelector('.prev-btn');
const nextBtn = document.querySelector('.next-btn');

let currentIndex = 2; // 默认中间项为初始激活项

// 初始化激活状态
updateCarousel();

prevBtn.addEventListener('click', () => {
  currentIndex = (currentIndex - 1 + items.length) % items.length;
  updateCarousel();
});

nextBtn.addEventListener('click', () => {
  currentIndex = (currentIndex + 1) % items.length;
  updateCarousel();
});

function updateCarousel() {
  // 先移除所有状态类名
  items.forEach(item => {
    item.classList.remove('active', 'prev', 'next');
  });

  // 给当前项加激活类
  items[currentIndex].classList.add('active');
  // 给前一项加prev类
  items[(currentIndex - 1 + items.length) % items.length].classList.add('prev');
  // 给后一项加next类
  items[(currentIndex + 1) % items.length].classList.add('next');

  // 调整轨道位置,确保中心项始终在容器中间
  const activeItem = items[currentIndex];
  const trackOffset = (track.offsetWidth / 2) - (activeItem.offsetWidth / 2) - activeItem.offsetLeft;
  track.style.transform = `translateX(${trackOffset}px)`;
}

// 窗口大小变化时重新计算位置,避免布局错乱
window.addEventListener('resize', updateCarousel);

优于你当前实现的点

  • 不限制轮播项数量,支持任意图片的循环轮播
  • 通过CSS过渡实现平滑切换,视觉效果更自然
  • 用类名控制状态,逻辑清晰,便于后续扩展(比如添加自动轮播、触摸滑动功能)
  • 自适应窗口尺寸,不会出现布局错位

内容的提问来源于stack exchange,提问作者Melonendk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 06:43:16