当前位置:首页 > JavaScript

js实现li轮播

2026-01-30 18:32:28JavaScript

实现 LI 轮播的 JavaScript 方法

HTML 结构 确保轮播内容包裹在 ulol 列表结构中,并为容器添加类名以便选择:

<div class="carousel-container">
  <ul class="carousel-list">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
  </ul>
  <button class="prev-btn">Previous</button>
  <button class="next-btn">Next</button>
</div>

CSS 样式 为轮播容器和列表项添加基础样式,确保隐藏非活动项:

.carousel-container {
  position: relative;
  overflow: hidden;
  width: 300px;
}
.carousel-list {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
  transition: transform 0.5s ease;
}
.carousel-list li {
  min-width: 100%;
  padding: 20px;
  box-sizing: border-box;
}

JavaScript 逻辑 通过修改 transform 属性实现平滑滑动效果:

document.addEventListener('DOMContentLoaded', () => {
  const list = document.querySelector('.carousel-list');
  const items = document.querySelectorAll('.carousel-list li');
  const prevBtn = document.querySelector('.prev-btn');
  const nextBtn = document.querySelector('.next-btn');
  let currentIndex = 0;

  function updateCarousel() {
    list.style.transform = `translateX(-${currentIndex * 100}%)`;
  }

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

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

自动轮播功能扩展

添加定时器实现自动播放,并优化鼠标交互时的暂停:

let autoPlayInterval = setInterval(() => {
  currentIndex = (currentIndex + 1) % items.length;
  updateCarousel();
}, 3000);

// 鼠标悬停时暂停轮播
document.querySelector('.carousel-container').addEventListener('mouseenter', () => {
  clearInterval(autoPlayInterval);
});

// 鼠标离开时恢复轮播
document.querySelector('.carousel-container').addEventListener('mouseleave', () => {
  autoPlayInterval = setInterval(() => {
    currentIndex = (currentIndex + 1) % items.length;
    updateCarousel();
  }, 3000);
});

响应式处理

通过监听窗口大小变化调整轮播布局:

function handleResize() {
  const containerWidth = document.querySelector('.carousel-container').offsetWidth;
  document.querySelectorAll('.carousel-list li').forEach(item => {
    item.style.minWidth = `${containerWidth}px`;
  });
  updateCarousel();
}

window.addEventListener('resize', handleResize);
handleResize(); // 初始化执行

指示器添加

创建动态指示点以增强用户体验:

const indicators = document.createElement('div');
indicators.className = 'carousel-indicators';
document.querySelector('.carousel-container').appendChild(indicators);

items.forEach((_, index) => {
  const dot = document.createElement('span');
  dot.addEventListener('click', () => {
    currentIndex = index;
    updateCarousel();
    updateIndicators();
  });
  indicators.appendChild(dot);
});

function updateIndicators() {
  const dots = document.querySelectorAll('.carousel-indicators span');
  dots.forEach((dot, index) => {
    dot.classList.toggle('active', index === currentIndex);
  });
}

js实现li轮播

标签: jsli
分享给朋友:

相关文章

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Pa…

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML结…

js实现复制功能

js实现复制功能

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

js实现百叶窗

js实现百叶窗

使用CSS和JavaScript实现百叶窗效果 通过CSS动画和JavaScript事件监听可以实现百叶窗效果。核心思路是将内容区域分割为多个条状元素,通过控制它们的展开/折叠状态来模拟百叶窗。…

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…