轮播图自动切换和导航切换

修改图片地址即可

<!DOCTYPE html>
<html lang="en">

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

    .outer {
      width: 320px;
      height: 212px;
      border: 1px solid blue;
      margin: 100px auto;
      position: relative;
    }

    .img-list {
      height: 100%;
    }

    .img-list li {
      position: absolute;
      opacity: 0;
      transition: opacity .3s;
    }

    img {
      width: 320px;
      vertical-align: none;
    }

    .img-list .current {
      z-index: 1;
      opacity: 1;
    }

    /* 箭头样式 */
    .prev-next a {
      height: 20px;
      font-size: 20px;
      color: #fff;
      text-decoration: none;
      font-weight: bold;
      position: absolute;
      margin: auto;
      top: 0;
      bottom: 0;
      z-index: 2;
      opacity: .5;
    }

    .prev-next a:hover {
      opacity: 1;
    }

    #next {
      right: 0;
    }

    /* 小圆点 */
    .dot {
      position: absolute;
      display: flex;
      justify-content: center;
      z-index: 3;
      left: 0;
      right: 0;
      bottom: 5px;
      margin: auto;
    }

    .dot a {
      width: 10px;
      height: 10px;
      margin: 5px;
      border-radius: 50%;
      background-color: #fff;
      opacity: .5;
    }

    .dot a:hover,
    .dot .active {
      opacity: 1;
    }
  </style>
</head>

<body>
  <div class="outer">
    <ul class="img-list">
      <li class="current">
        <a href="javascript:;">
          <img src="./img/3.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="javascript:;">
          <img src="./img/4.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="javascript:;">
          <img src="./img/5.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="javascript:;">
          <img src="./img/6.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="javascript:;">
          <img src="./img/7.jpg" alt="">
        </a>
      </li>
    </ul>

    <div class="prev-next">
      <a href="javascript:;" id="prev"></a>
      <a href="javascript:;" id="next"></a>
    </div>

    <!-- 小圆点 -->
    <div class="dot">
      <a href="javascript:;" class="active"></a>
      <a href="javascript:;"></a>
      <a href="javascript:;"></a>
      <a href="javascript:;"></a>
      <a href="javascript:;"></a>
    </div>
  </div>

  <script>
    // 获取小圆点
    const dots = Array.from(document.querySelectorAll(".dot a"));
    const imgArr = Array.from(document.querySelectorAll(".img-list li"))



    document.addEventListener('click', (event) => {
      const index = dots.indexOf(event.target);

      if (index !== -1) {
        changeImg(index);

      }
    })
    const toggleChange = (function () {
      let timer = null;
      return () => {
        if (timer === null) {
          timer = setTimeout(function auto() {
            changeImg("next");
            timer = setTimeout(auto, 3000)
          }, 3000)
        } else {
          clearTimeout(timer);
          timer = null;
        }
      }
    })()

    toggleChange()

    // 鼠标移入不切换图片
    const outer = document.getElementsByClassName("outer")[0];
    outer.onmouseenter = function () {
      toggleChange();
    }
    outer.onmouseleave = () => {
      toggleChange();
    }
    // 点击按钮切换图片
    const prev = document.getElementById("prev");
    const next = document.getElementById("next");



    prev.onclick = function () {
      changeImg("prev");
    }
    next.onclick = function () {
      changeImg("next");
    }

    function changeImg(dir) {

      // 获取当前显示的图片
      const current = document.querySelector(".img-list .current");




      // 获取下一张图片
      let next;
      if (dir === "next") {
        next = current.nextElementSibling ||
          imgArr[0];
      } else if (dir === "prev") {
        next = current.previousElementSibling || imgArr.at(-1);
      } else if (typeof dir === "number") {//传的索引
        next = imgArr[dir];


      }

      // 获取当前要现实的图片的索引
      const index = imgArr.indexOf(next);

      // 切换显示状态
      current.classList.remove("current")
      next.classList.add("current")

      // 切换active
      const currentActive = document.querySelector(".active");
      currentActive.classList.remove("active");
      // 获取当前要显示的小圆点
      dots[index].classList.add("active");

    }
  </script>
</body>

</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值