(八)原生js案例之图片轮播显示设置背景

图片轮播,看到自己喜欢的壁纸,进行选择设置

效果

请添加图片描述

实现代码

  • 必要的css
* {
      margin: 0;
      padding: 0;
    }

    body {
      height: 100vh;
      overflow: hidden;
    }

    #box {
      width: 800px;
      height: 400px;
      border: 1px solid #ccc;
      margin: 60px auto 20px;
    }
  • 静态页面
<div id="box">
   
  </div>
  <ul id="list" class="order-list d-flex justify-content-center">
    <li class="list-group-item d-flex align-items-center">
      <button type="button" class="btn btn-primary mx-2">图片播放</button>
      <button type="button" class="btn btn-danger mx-2">选择确认</button>
    </li>
  </ul>
  • 核心代码
window.onload = function () {
      const oBtn = document.querySelectorAll('button');
      const picArr = ['./images/1.png', './images/2.png', './images/3.png'];
      const oBody = document.body;
      const oBox = document.getElementById('box');
      let index = 0, timer = null;

      oBtn[0].onclick = function () {
        clearInterval(timer);
        //定时更换背景图片
        function changeBg() {
          //实现图片循环切换
          index = index % 3
          oBox.index = index
          oBox.style.background = `url(${picArr[index]}) no-repeat center center`;
          index++;
        }
        timer = setInterval(changeBg, 1000)
      }

      //取消定时器
      oBtn[1].onclick = function () {
        clearInterval(timer);
        oBody.style.background = `url(${picArr[oBox.index]}) no-repeat center center`;
      }
    }

相关推荐

  1. android 图片

    2024-07-20 12:00:04       17 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 12:00:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 12:00:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 12:00:04       45 阅读
  4. Python语言-面向对象

    2024-07-20 12:00:04       55 阅读

热门阅读

  1. 【AI工具基础】—Kylin(一)

    2024-07-20 12:00:04       17 阅读
  2. Unity3D 如何读取策划给定的Excel表格详解

    2024-07-20 12:00:04       19 阅读
  3. PHP学习笔记③

    2024-07-20 12:00:04       14 阅读
  4. YOLO 各版本对比

    2024-07-20 12:00:04       18 阅读
  5. 基于gunicorn+flask+docker模型 高并发部署

    2024-07-20 12:00:04       17 阅读
  6. 跟着GPT学设计模式之模板模式

    2024-07-20 12:00:04       18 阅读
  7. 动态美学:WebKit中CSS转换与动画的魔力

    2024-07-20 12:00:04       17 阅读
  8. 华为AI认证必刷题100道(上)

    2024-07-20 12:00:04       19 阅读
  9. 概率论中的卷积公式

    2024-07-20 12:00:04       13 阅读
  10. 数据结构:栈

    2024-07-20 12:00:04       15 阅读
  11. 数据质量8个衡量标准

    2024-07-20 12:00:04       14 阅读