当前位置:首页 > JavaScript

js 实现图片轮播

2026-01-16 12:19:26JavaScript

基础实现方案

使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS负责样式布局,JavaScript处理轮播逻辑。

<div class="slider">
  <img src="image1.jpg" class="active">
  <img src="image2.jpg">
  <img src="image3.jpg">
</div>
.slider {
  position: relative;
  width: 500px;
  height: 300px;
  overflow: hidden;
}
.slider img {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 1s;
}
.slider img.active {
  opacity: 1;
}
let currentIndex = 0;
const images = document.querySelectorAll('.slider img');
const totalImages = images.length;

function showNextImage() {
  images[currentIndex].classList.remove('active');
  currentIndex = (currentIndex + 1) % totalImages;
  images[currentIndex].classList.add('active');
}

setInterval(showNextImage, 3000);

添加导航控制

扩展基础功能,增加前进/后退按钮和指示器圆点。修改HTML结构添加控制元素。

js 实现图片轮播

<div class="slider">
  <div class="slider-container">
    <img src="image1.jpg" class="active">
    <img src="image2.jpg">
    <img src="image3.jpg">
  </div>
  <button class="prev">←</button>
  <button class="next">→</button>
  <div class="dots">
    <span class="dot active" data-index="0"></span>
    <span class="dot" data-index="1"></span>
    <span class="dot" data-index="2"></span>
  </div>
</div>
const prevBtn = document.querySelector('.prev');
const nextBtn = document.querySelector('.next');
const dots = document.querySelectorAll('.dot');

function updateControls() {
  dots.forEach(dot => dot.classList.remove('active'));
  dots[currentIndex].classList.add('active');
}

nextBtn.addEventListener('click', () => {
  showNextImage();
  updateControls();
});

prevBtn.addEventListener('click', () => {
  images[currentIndex].classList.remove('active');
  currentIndex = (currentIndex - 1 + totalImages) % totalImages;
  images[currentIndex].classList.add('active');
  updateControls();
});

dots.forEach(dot => {
  dot.addEventListener('click', () => {
    images[currentIndex].classList.remove('active');
    currentIndex = parseInt(dot.dataset.index);
    images[currentIndex].classList.add('active');
    updateControls();
  });
});

触摸滑动支持

为移动设备添加触摸事件支持,实现手势滑动切换。

const slider = document.querySelector('.slider-container');
let touchStartX = 0;
let touchEndX = 0;

slider.addEventListener('touchstart', (e) => {
  touchStartX = e.changedTouches[0].screenX;
});

slider.addEventListener('touchend', (e) => {
  touchEndX = e.changedTouches[0].screenX;
  handleSwipe();
});

function handleSwipe() {
  const threshold = 50;
  if (touchEndX < touchStartX - threshold) {
    showNextImage();
    updateControls();
  }
  if (touchEndX > touchStartX + threshold) {
    images[currentIndex].classList.remove('active');
    currentIndex = (currentIndex - 1 + totalImages) % totalImages;
    images[currentIndex].classList.add('active');
    updateControls();
  }
}

自动播放与暂停

改进自动播放功能,增加鼠标悬停暂停效果。

js 实现图片轮播

let autoplayInterval;

function startAutoplay() {
  autoplayInterval = setInterval(() => {
    showNextImage();
    updateControls();
  }, 3000);
}

function stopAutoplay() {
  clearInterval(autoplayInterval);
}

slider.addEventListener('mouseenter', stopAutoplay);
slider.addEventListener('mouseleave', startAutoplay);

startAutoplay();

响应式设计

通过CSS媒体查询使轮播适应不同屏幕尺寸。

@media (max-width: 768px) {
  .slider {
    width: 100%;
    height: 200px;
  }
  .prev, .next {
    display: none;
  }
}

无限循环优化

修改切换逻辑实现无缝无限循环效果,避免最后一页跳转时的生硬感。

function showNextImage() {
  images[currentIndex].classList.remove('active');
  const nextIndex = (currentIndex + 1) % totalImages;

  // 预加载下一张图片
  images[nextIndex].classList.add('next');
  setTimeout(() => {
    images[nextIndex].classList.remove('next');
    images[nextIndex].classList.add('active');
    currentIndex = nextIndex;
    updateControls();
  }, 10);
}

标签: 图片js
分享给朋友:

相关文章

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template>…

js实现

js实现

实现 JavaScript 功能的方法 在 JavaScript 中实现特定功能通常涉及多个步骤。以下是常见的实现方法: 基础语法和变量声明 使用 let 或 const 声明变量: let…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取 j…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,包括静态资源引用、动态绑定、懒加载等。以下是几种常见的方法: 静态资源引用 将图片放在项目的 public 或…

js实现预览

js实现预览

文件上传预览实现 使用JavaScript实现文件上传预览功能,可以通过FileReader对象读取文件内容并显示预览。以下代码支持图片、文本和PDF文件的预览: // HTML部分需要包含inp…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-…