原生JS实现小米风格轮播图完整教程

作者:纸飞机

时间:2026-10-23

浏览:89

本文详细介绍如何使用原生JavaScript实现一个仿小米风格的轮播图组件。内容涵盖HTML结构搭建、CSS样式布局以及核心JS逻辑,包括自动轮播定时器、上一张/下一张按钮点击事件及底部小圆点导航切换功能。通过完整代码示例,帮助开发者掌握轮播图的核心实现原理与DOM操作方法。

文章目录

原生JS轮播图实现概述

本文提供基于原生JavaScript编写的仿小米风格轮播图代码,该版本注重易读性与实用性,供广大开发者参考学习。

本示例主要实现以下交互功能:

  • 支持图片自动循环播放。
  • 提供“上一张”和“下一张”按钮,点击可手动切换图片。
  • 支持点击底部导航小圆点,直接跳转到对应图片。

完整代码实现

具体实现代码分为HTML、CSS和JavaScript三个部分,如下所示。

HTML结构搭建

H5: 5


<div class="wrap">
    <ul class="list">
      <li class="item active"><img src="img/001.jpg" alt=""></li>
      <li class="item"><img src="img/002.jpg"" alt=""></li>
      <li class="item"><img src="img/001.jpg" alt=""></li>
      <li class="item"><img src="img/002.jpg" alt=""></li>
      <li class="item"><img src="img/001.jpg" alt=""></li>
    </ul>
    <!-- 小圆点 -->
    <ul class="pointList">
      <li class="point active" data-index="0"></li>
      <li class="point" data-index="1"></li>
      <li class="point"data-index="2"></li>
      <li class="point"data-index="3"></li>
      <li class="point"data-index="4"></li>
    </ul>
    <button type="button" class="btn" id="goPre"><</button>
    <button type="button" class="btn" id="goNext">></button>
</div>

CSS样式设计

CSS样式部分:


<style>
    .wrap {
      width: 800px;
      height: 400px;
      position: relative;
    }

    .list {
      width: 800px;
      height: 400px;
      list-style: none;
      position: relative;
      padding-left: 0px;
    }

    .item {
      position: absolute;
      width: 100%;
      height: 100%;
      color: white;
      font-size: 50px;
      opacity: 0.6;
      transform: all .8s;
    }
    .item img{
      width: 800px;
      height: 400px;
    }
    .btn {
      width: 50px;
      height: 100px;
      position: absolute;
      top: 150px;
      z-index: 100;
    }

    #goPre {
      left: 0px;
    }

    #goNext {
      right: 0px;
    }

    .item.active {
      opacity: 1;
      z-index: 10;
    }

    .pointList{
      padding-left: 0;
      list-style: none;
      position: absolute;
      right: 20px;
      bottom: 20px;
      z-index: 1000;
    }
    .point{
      width: 10px;
      height: 10px;
      background-color: rgba(0,0,0,0.4);
      border-radius: 50%;
      float: left;
      margin-right: 16px;
      border-style: solid;
      border-width: 2px;
      border-color: rgba(255,255,255, 0.6);
      cursor: pointer;
    }
    .point.active{
      background-color: rgba(255,255,255,0.4);
    }
</style>

JavaScript核心逻辑

JavaScript逻辑部分:


<script>
    //获取节点
    var items = document.getElementsByClassName('item')//图片
    var goPreBtn = document.getElementById('goPre');
    var goNextBtn = document.getElementById('goNext');
    //获取点
    var points=document.getElementsByClassName('point');

   var time=0;//定时器图片跳转参数

    var index = 0; //表示第几张图片在展示
    //可以展示第几个点

    var clearActive=function(){
      for(var i=0;i<items.length;i++){
        items[i].className='item';
        points[i].className='point';
      }
    }

    var goIndex=function(){
      clearActive();
      items[index].className='item active';
      points[index].className='point active';
    }

    var goNext=function(){
      if(index<4){
        index++;
      }else{
        index=0;
      }
      goIndex();
    }
    var goPre=function(){
      if(index==0){
        index=4;
      }else{
        index--;
      }
      goIndex();
    }

    //点击下一张切换图片
    goNextBtn.addEventListener('click' ,function(){
      goNext();
      time=0;
    })
  //点击上一张进行切换图片
    goPreBtn.addEventListener('click' ,function(){
      goPre();
      time=0;
    })

   for(var i=0;i<points.length;i++){
    points[i].addEventListener('click',function(){
      var pointIndex=this.getAttribute('data-index');
      index=pointIndex;
      goIndex();
      time=0;
    })
   }
   
  //自动轮播定时器
  setInterval(function(){
    time++;
    if(time==20){
      goNext();
      time=0;
    } 
  },100)
</script>

以上即为本文的全部内容,希望这些代码能对各位的学习有所帮助,欢迎继续支持本站获取更多技术资源。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章