当前位置:首页 > JavaScript

js实现div轮播

2026-01-30 11:08:07JavaScript

实现基础轮播结构

HTML部分需创建一个包含轮播项的容器及导航按钮:

<div class="slider-container">
  <div class="slider-track">
    <div class="slide-item">Slide 1</div>
    <div class="slide-item">Slide 2</div>
    <div class="slide-item">Slide 3</div>
  </div>
  <button class="prev-btn">❮</button>
  <button class="next-btn">❯</button>
</div>

CSS部分设置布局与动画效果:

js实现div轮播

.slider-container {
  position: relative;
  width: 600px;
  height: 300px;
  overflow: hidden;
}
.slider-track {
  display: flex;
  transition: transform 0.5s ease;
}
.slide-item {
  min-width: 100%;
  height: 100%;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
}
button {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
}
.prev-btn { left: 10px; }
.next-btn { right: 10px; }

核心JavaScript逻辑

初始化变量并实现滑动功能:

js实现div轮播

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

let currentIndex = 0;
const slideWidth = slides[0].getBoundingClientRect().width;

const updateSlidePosition = () => {
  track.style.transform = `translateX(-${currentIndex * slideWidth}px)`;
};

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

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

自动轮播功能

添加定时自动切换功能:

let autoSlideInterval;

const startAutoSlide = (interval = 3000) => {
  autoSlideInterval = setInterval(() => {
    currentIndex = (currentIndex + 1) % slides.length;
    updateSlidePosition();
  }, interval);
};

const stopAutoSlide = () => {
  clearInterval(autoSlideInterval);
};

// 鼠标悬停时暂停轮播
track.parentElement.addEventListener('mouseenter', stopAutoSlide);
track.parentElement.addEventListener('mouseleave', () => startAutoSlide());

// 初始化自动轮播
startAutoSlide();

无限循环优化

克隆首尾元素实现无缝循环:

const setupInfiniteSlide = () => {
  const firstClone = slides[0].cloneNode(true);
  const lastClone = slides[slides.length - 1].cloneNode(true);

  track.appendChild(firstClone);
  track.insertBefore(lastClone, slides[0]);

  track.style.transform = `translateX(-${slideWidth}px)`;
};

setupInfiniteSlide();

// 修改切换逻辑处理克隆节点
nextBtn.addEventListener('click', () => {
  if (currentIndex >= slides.length - 1) return;
  currentIndex++;
  updateSlidePosition();
});

track.addEventListener('transitionend', () => {
  if (slides[currentIndex].classList.contains('clone')) {
    track.style.transition = 'none';
    currentIndex = currentIndex === 0 ? slides.length - 2 : 1;
    updateSlidePosition();
    setTimeout(() => track.style.transition = 'transform 0.5s ease');
  }
});

标签: jsdiv
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 //…

vue.js实现轮播

vue.js实现轮播

Vue.js 实现轮播功能 使用第三方库(推荐) Vue.js 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper,它们功能丰富且易于集成。 安装 swip…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https:/…

js实现验证码

js实现验证码

使用Canvas生成图形验证码 在HTML中创建一个Canvas元素用于绘制验证码。通过JavaScript随机生成数字或字母组合,并添加干扰线、噪点等干扰元素增强安全性。 <canvas i…

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前…

js实现类

js实现类

JavaScript 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统的…