当前位置:首页 > JavaScript

js 实现走马灯

2026-04-04 07:29:06JavaScript

实现走马灯效果

走马灯(轮播图)可以通过纯 JavaScript 实现,以下是两种常见的方法:

js 实现走马灯

使用 CSS 动画和 JavaScript 控制

// HTML 结构示例
<div class="carousel">
  <div class="carousel-items">
    <div class="carousel-item">Item 1</div>
    <div class="carousel-item">Item 2</div>
    <div class="carousel-item">Item 3</div>
  </div>
</div>
.carousel {
  width: 100%;
  overflow: hidden;
}
.carousel-items {
  display: flex;
  transition: transform 0.5s ease;
}
.carousel-item {
  min-width: 100%;
}
const carousel = document.querySelector('.carousel-items');
let currentIndex = 0;
const items = document.querySelectorAll('.carousel-item');
const itemCount = items.length;

function moveToNext() {
  currentIndex = (currentIndex + 1) % itemCount;
  carousel.style.transform = `translateX(-${currentIndex * 100}%)`;
}

// 设置自动轮播
setInterval(moveToNext, 3000);

纯 JavaScript 实现

function createCarousel(containerId, items, interval = 3000) {
  const container = document.getElementById(containerId);
  let currentIndex = 0;

  // 创建轮播元素
  const carousel = document.createElement('div');
  carousel.className = 'carousel';
  carousel.style.display = 'flex';
  carousel.style.transition = 'transform 0.5s ease';

  items.forEach(item => {
    const element = document.createElement('div');
    element.className = 'carousel-item';
    element.textContent = item;
    element.style.minWidth = '100%';
    carousel.appendChild(element);
  });

  container.appendChild(carousel);

  // 轮播函数
  function slide() {
    currentIndex = (currentIndex + 1) % items.length;
    carousel.style.transform = `translateX(-${currentIndex * 100}%)`;
  }

  // 启动轮播
  let timer = setInterval(slide, interval);

  // 添加暂停功能
  container.addEventListener('mouseenter', () => clearInterval(timer));
  container.addEventListener('mouseleave', () => {
    timer = setInterval(slide, interval);
  });
}

// 使用示例
createCarousel('myCarousel', ['Item 1', 'Item 2', 'Item 3']);

添加导航按钮

function addNavigation(carouselId) {
  const container = document.getElementById(carouselId);
  const carousel = container.querySelector('.carousel');
  const items = container.querySelectorAll('.carousel-item');
  let currentIndex = 0;

  // 创建导航按钮
  const prevBtn = document.createElement('button');
  prevBtn.textContent = '←';
  const nextBtn = document.createElement('button');
  nextBtn.textContent = '→';

  container.appendChild(prevBtn);
  container.appendChild(nextBtn);

  // 导航功能
  function updateCarousel() {
    carousel.style.transform = `translateX(-${currentIndex * 100}%)`;
  }

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

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

响应式设计考虑

function makeResponsive(carouselId) {
  const container = document.getElementById(carouselId);
  const carousel = container.querySelector('.carousel');
  const items = container.querySelectorAll('.carousel-item');

  function resizeHandler() {
    const containerWidth = container.clientWidth;
    items.forEach(item => {
      item.style.width = `${containerWidth}px`;
    });
    carousel.style.width = `${containerWidth * items.length}px`;
  }

  // 初始设置
  resizeHandler();

  // 窗口大小变化时调整
  window.addEventListener('resize', resizeHandler);
}

这些方法提供了基本的走马灯功能,可以根据需求进一步扩展,如添加指示器、淡入淡出效果等。

标签: 走马灯js
分享给朋友:

相关文章

js实现轮播

js实现轮播

实现轮播图的基本思路 轮播图的核心逻辑是通过定时切换展示的图片或内容,通常结合左右滑动按钮和指示器(小圆点)增强交互性。以下是基于原生JavaScript的实现方法。 HTML结构 构建轮播图的HT…

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Jav…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(func,…

使用js实现

使用js实现

使用JavaScript实现 JavaScript可以通过Math.random()函数生成随机数,结合其他方法可以实现多种随机数生成需求。 // 生成0到1之间的随机小数 const random…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…

js实现图片放大

js实现图片放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性实现图片放大效果。当鼠标悬停时,图片会平滑放大。 const img = document…