手写轮播图

不废话直接上干货,图片地址换成自己的链接

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    ul, ol, li {
      list-style: none;
    }

    img {
      width: 100%;
      height: 100%;
      display: block;
    }

    .banner {
      width: 100%;
      height: 500px;
      position: relative;
      margin: 50px 0;
    }

    .banner > ul {
      width: 100%;
      height: 100%;

      position: relative;
    }

    .banner > ul > li {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;

      opacity: 0;

      transition: opacity .5s linear;
    }

    .banner > ul > li.active {
      opacity: 1;
    }

    .banner > ol {
      width: 200px;
      height: 30px;
      position: absolute;
      left: 30px;
      bottom: 30px;
      background-color: rgba(0, 0, 0, .5);

      display: flex;
      justify-content: space-around;
      align-items: center;
      border-radius: 15px;
    }

    .banner > ol > li {
      width: 20px;
      height: 20px;
      background-color: #fff;
      border-radius: 50%;
      cursor: pointer;
    }

    .banner > ol > li.active {
      background-color: orange;
    }

    .banner > div {
      width: 40px;
      height: 60px;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      background-color: rgba(0, 0, 0, .5);

      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
      color: #fff;
      cursor: pointer;
    }

    .banner > div.left {
      left: 0;
    }

    .banner > div.right {
      right: 0;
    }
  </style>
</head>
<body>

  <div class="banner">
    <!-- 图片区域 -->
    <ul class="imgBox">
      <li class="active"><img src="./imgs/01.jpg" alt=""></li>
      <li><img src="./imgs/02.jpg" alt=""></li>
      <li><img src="./imgs/03.jpg" alt=""></li>
      <li><img src="./imgs/04.jpg" alt=""></li>
    </ul>

    <!-- 焦点区域 -->
    <ol>
      <li data-i="0" data-name="point" class="active"></li>
      <li data-i="1" data-name="point"></li>
      <li data-i="2" data-name="point"></li>
      <li data-i="3" data-name="point"></li>
    </ol>

    <!-- 左右切换按钮 -->
    <div class="left">&lt;</div>
    <div class="right">&gt;</div>
  </div>

  <script>
    // 获取到所有承载图片的 li 和所有承载焦点的 li
    var imgs = document.querySelectorAll('ul > li')
    var points = document.querySelectorAll('ol > li')
    var banner = document.querySelector('.banner')

    // 准备一个变量, 表示当前是第几张, 默认是 0, 因为默认显示的是索引第 0 张
    var index = 0

    // 书写一个切换一张的函数
    // 约定:
    //   参数为 true, 我们切换下一张
    //   参数为 false, 我们切换上一张
    //   参数为 数字, 我们切换到指定索引的那一张
    function changeOne(type) {
      // 1. 让当前这一张消失
      imgs[index].className = ''
      points[index].className = ''

      // 2. 根据 type 传递来的参数, 来切换 index 的值
      if (type === true) {
        index++
      } else if (type === false) {
        index--
      } else {
        index = type
      }
      // 判定一下 index 的边界值
      if (index >= imgs.length) {
        index = 0
      }
      if (index < 0) {
        index = imgs.length - 1
      }

      // 3. 让改变后的这一张显示出来
      imgs[index].className = 'active'
      points[index].className = 'active'
    }

    // 给 轮播图区域 盒子绑定点击事件
    banner.onclick = function (e) {

      // 判断点击的是左按钮
      if (e.target.className === 'left') {
        console.log('点击的是左按钮')
        // 切换上一张, 调用 changeOne 方法传递参数为 false
        changeOne(false)
      }

      // 判断点击的是右按钮
      if (e.target.className === 'right') {
        console.log('点击的是右按钮')
        // 切换下一张, 调用 changeOne 方法传递参数为 true
        changeOne(true)
      }

      // 判断点击的是焦点盒子
      if (e.target.dataset.name === 'point') {
        console.log('点击的是焦点盒子')
        // 拿到自己身上记录的索引
        var i = e.target.dataset.i - 0
        // 切换某一张, 调用 changeOne 方法传递参数为要切换的索引
        changeOne(i)
      }

    }

    // 自动切换功能
    setInterval(function () {

      // 切换到下一张
      changeOne(true)

    }, 5000)

  </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq614756883

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值