当前位置:首页 > JavaScript

js实现焦点轮播图

2026-01-30 16:06:16JavaScript

实现焦点轮播图的核心思路

轮播图的核心逻辑是通过定时切换图片的显示状态,结合用户交互(如点击焦点按钮)实现手动切换。以下是基于原生JavaScript的实现方法。

HTML结构

创建一个包含图片容器和焦点按钮的基础结构:

<div class="slider">
  <div class="slider-container">
    <img src="image1.jpg" class="active">
    <img src="image2.jpg">
    <img src="image3.jpg">
  </div>
  <div class="indicator">
    <span class="dot active"></span>
    <span class="dot"></span>
    <span class="dot"></span>
  </div>
</div>

CSS样式

设置基本布局和过渡效果:

.slider {
  position: relative;
  width: 600px;
  height: 400px;
  overflow: hidden;
}
.slider-container img {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 1s;
}
.slider-container img.active {
  opacity: 1;
}
.indicator {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
}
.dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  margin: 0 5px;
  border-radius: 50%;
  background: rgba(255,255,255,0.5);
  cursor: pointer;
}
.dot.active {
  background: white;
}

JavaScript逻辑

初始化变量

const container = document.querySelector('.slider-container');
const images = document.querySelectorAll('.slider-container img');
const dots = document.querySelectorAll('.dot');
let currentIndex = 0;
let timer = null;

图片切换函数

function showImage(index) {
  images.forEach(img => img.classList.remove('active'));
  dots.forEach(dot => dot.classList.remove('active'));

  images[index].classList.add('active');
  dots[index].classList.add('active');
  currentIndex = index;
}

自动轮播功能

function startAutoPlay() {
  timer = setInterval(() => {
    const nextIndex = (currentIndex + 1) % images.length;
    showImage(nextIndex);
  }, 3000);
}

function stopAutoPlay() {
  clearInterval(timer);
}

焦点按钮事件绑定

dots.forEach((dot, index) => {
  dot.addEventListener('click', () => {
    stopAutoPlay();
    showImage(index);
    startAutoPlay();
  });
});

初始化轮播

showImage(0);
startAutoPlay();

// 鼠标悬停暂停
container.addEventListener('mouseenter', stopAutoPlay);
container.addEventListener('mouseleave', startAutoPlay);

完整代码示例

将所有代码整合后形成完整实现:

document.addEventListener('DOMContentLoaded', function() {
  const container = document.querySelector('.slider-container');
  const images = document.querySelectorAll('.slider-container img');
  const dots = document.querySelectorAll('.dot');
  let currentIndex = 0;
  let timer = null;

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

  function startAutoPlay() {
    timer = setInterval(() => {
      const nextIndex = (currentIndex + 1) % images.length;
      showImage(nextIndex);
    }, 3000);
  }

  function stopAutoPlay() {
    clearInterval(timer);
  }

  dots.forEach((dot, index) => {
    dot.addEventListener('click', () => {
      stopAutoPlay();
      showImage(index);
      startAutoPlay();
    });
  });

  showImage(0);
  startAutoPlay();

  container.addEventListener('mouseenter', stopAutoPlay);
  container.addEventListener('mouseleave', startAutoPlay);
});

扩展功能建议

  • 添加左右箭头导航
  • 实现无限循环滑动效果
  • 增加触摸滑动支持(移动端)
  • 添加过渡动画效果(如淡入淡出、滑动等)
  • 响应式布局适配不同屏幕尺寸

通过以上代码可实现一个基础的焦点轮播图,可根据实际需求进一步扩展功能。核心要点在于通过CSS控制显示状态,JavaScript管理切换逻辑,并处理好用户交互与自动播放的关系。

js实现焦点轮播图

标签: 焦点js
分享给朋友:

相关文章

vue 实现获取焦点

vue 实现获取焦点

实现获取焦点的方法 在 Vue 中实现获取焦点可以通过多种方式完成,以下是几种常见的方法: 使用 ref 和 focus 方法 通过 ref 属性获取 DOM 元素,并调用其 focus 方法实现焦…

js 实现vue模板

js 实现vue模板

实现 Vue 模板的 JavaScript 方法 通过原生 JavaScript 可以实现类似 Vue 的模板渲染功能,主要包括数据绑定、指令处理和模板解析。以下是核心实现思路: 数据绑定与响应式…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document…

js实现全选

js实现全选

实现全选功能的方法 在JavaScript中实现全选功能通常涉及监听全选复选框的点击事件,并根据其状态控制其他复选框的选中状态。以下是几种常见的实现方式: 基础DOM操作实现 通过获取所有目标复选…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

js如何实现继承

js如何实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例能够访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…