(一)原生js案例之图片轮播

原生js实现的两种播放效果

效果一

循环播放,单一的效果

代码实现

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图片切换</title>
  <style>
    *{
      padding: 0;
      margin: 0;
    }
    #content{
      width: 400px;
      height: 400px;
      border: 1px solid #000;
      margin: 50px auto;
      position: relative;
      background-color: aliceblue;
  }
    #content a{
      width: 40px;
      height: 40px;
      background-color: #000;
      border: 5px solid #fff;
      position: absolute;
      top: 175px;
      text-decoration: none;
      line-height: 40px;
      text-align: center;
      color: #fff;
      filter: opacity(50);
      opacity: 0.5;
    }
    #content a:hover{
      filter: opacity(100);
      opacity: 1;
    }
    #prev{left: 10px;}
    #next{ right: 10px;}
    #desc,#num{
      position: absolute;
      left: 0;
      width: 100%;
      height: 30px;
      line-height: 30px;
      box-sizing: border-box;
      background-color: #000;
      color: #fff;
      filter: opacity(50);
      opacity: 0.5;
      padding: 0 10px;
    }
    #desc{bottom: 0;}
    #img{width: 400px;height: 400px;}
    #num{filter: opacity(.9);
      opacity: 90;
      text-align: center;
    }
  </style>
  <script>
    window.onload = function () {
      const oPrev = document.getElementById('prev');
      const oNext = document.getElementById('next');
      const oImg = document.getElementsByTagName('img')[0];
      const oDesc = document.getElementById('desc');
      const oNum = document.getElementById('num');
      const oImgs = ['images/img1.png', 'images/img2.png', 'images/img3.png', 'images/img4.png'];
      const oText = ["这是第一张图片", "这是第二张图片", "这是第三张图片", "这是第四张图片"]
      let num = 0;

      
      tabChange()

      oPrev.addEventListener('click', function(){
        num--
        if(num < 0){
          num = oImgs.length-1;
        }
        tabChange()

      })

      oNext.addEventListener('click', function(){
        num++
        if(num == oImgs.length){
          num = 0;
        }
        tabChange()
      })
      function tabChange(){
        //计算图片数量
        oNum.innerHTML = num+1 + "/" + oImgs.length;
        oImg.src = oImgs[num];
        oDesc.innerHTML = oText[num];
      }


    }
  </script>
</head>
<body>
  <div id="content">
    <a id="prev" href="javascript:;"><</a>
    <img id="img" alt="">
    <p id="desc">图片文字加载中...</p>
    <span id="num">正在计算中...</span>
    <a id="next" href="javascript:;">></a>
  </div>
</body>
</html>

效果二

可以控制是否轮播播放

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图片切换</title>
  <style>
    *{
      padding: 0;
      margin: 0;
    }
    #content{
      width: 400px;
      height: 400px;
      border: 1px solid #000;
      margin: 50px auto;
      position: relative;
      background-color: aliceblue;
  }
    #content a{
      width: 40px;
      height: 40px;
      background-color: #000;
      border: 5px solid #fff;
      position: absolute;
      top: 175px;
      text-decoration: none;
      line-height: 40px;
      text-align: center;
      color: #fff;
      filter: opacity(50);
      opacity: 0.5;
    }
    #content a:hover{
      filter: opacity(100);
      opacity: 1;
    }
    #prev{left: 10px;}
    #next{ right: 10px;}
    #desc,#num{
      position: absolute;
      left: 0;
      width: 100%;
      height: 30px;
      line-height: 30px;
      box-sizing: border-box;
      background-color: #000;
      color: #fff;
      filter: opacity(50);
      opacity: 0.5;
      padding: 0 10px;
    }
    #desc{bottom: 0;}
    #img{width: 400px;height: 400px;}
    #num{filter: opacity(.9);
      opacity: 90;
      text-align: center;
    }
    .tool{
      text-align: center;
    }
  </style>
  <script>
    window.onload = function () {
      const oPrev = document.getElementById('prev');
      const oNext = document.getElementById('next');
      const oImg = document.getElementsByTagName('img')[0];
      const oDesc = document.getElementById('desc');
      const oNum = document.getElementById('num');
      const oLoop = document.getElementById('loop');
      const oStop = document.getElementById('stop');
      const oImgs = ['images/img1.png', 'images/img2.png', 'images/img3.png', 'images/img4.png'];
      const oText = ["这是第一张图片", "这是第二张图片", "这是第三张图片", "这是第四张图片"]
      let num = 0;
      let isLoop = true;


      oLoop.addEventListener('click', function(){
        isLoop = true;
      })

      oStop.addEventListener('click', function(){
        isLoop = false;
      })


      
      tabChange()

      oPrev.addEventListener('click', function(){
        num--
        if(num < 0){
          if(isLoop){
            num = oImgs.length-1;
          }else{
            alert("没有上一页了")
            num = 0;
            return false;
          }
        }
        tabChange()

      })

      oNext.addEventListener('click', function(){
        num++
        if(num == oImgs.length){
          if(isLoop){
            num = 0;
          }else{
            alert("没有下一页了")
            num = oImgs.length-1;
            return false;
          }
        }
        tabChange()
      })
      function tabChange(){
        //计算图片数量
        oNum.innerHTML = num+1 + "/" + oImgs.length;
        oImg.src = oImgs[num];
        oDesc.innerHTML = oText[num];
      }


    }
  </script>
</head>
<body>
  <div class="tool">
    <h1>图片切换</h1>
    <button id="loop">循环切换</button>
    <button id="stop">停止循环切换</button>
  </div>
  <div id="content">
    <a id="prev" href="javascript:;"><</a>
    <img id="img" alt="">
    <p id="desc">图片文字加载中...</p>
    <span id="num">正在计算中...</span>
    <a id="next" href="javascript:;">></a>
  </div>
</body>
</html>
···
相关推荐
LucianaiB1 分钟前
拿到Offer,租房怎么办?看我用高德MCP+腾讯云MCP,帮你分分钟搞定!
前端·后端·cursor
用户17592342150287 分钟前
D3.js - 基本用法
前端·d3.js
Mr.Liu623 分钟前
小程序30-wxml语法-声明和绑定数据
前端·微信小程序·小程序
76756047924 分钟前
useDateFormat源码解析
前端·源码
Mintopia24 分钟前
Three.js粒子系统开发实战:从基础到性能优化
前端·javascript·three.js
Promise52025 分钟前
大屏"跑马灯" 长列表性能优化
前端·javascript
子玖25 分钟前
初始化项目前的准备
前端·javascript·vue.js
Mintopia25 分钟前
Three.js进阶实战:打造动态光影交互场景 ——结合环境光、聚光灯与相机控制的沉浸式体验
前端·javascript·three.js
贵州数擎科技有限公司26 分钟前
Threejs绘制小兩伞快拿去送给你的女神
前端
Carlos_sam28 分钟前
OpenLayers:封装Overlay的方法
前端·javascript