切片式轮播图

借鉴自时迁酱

css

            * {
        margin: 0;
        padding: 0;
      }

      li {
        list-style: none;
      }

      body {
        display: flex;
        height: 100vh;
        justify-content: center;
        align-items: center;
        background: radial-gradient(circle at top, #444, #000);
      }

      .main {
        display: flex;
        -webkit-box-reflect: below 13px linear-gradient(transparent 70%,rgba(0,0,0,0.2));
      }

      .box {
        width: 900px;
        height: 500px;
        background-size: cover;
        transition: all 0.5s;
      }

      .box img {
        width: 900px;
        height: 500px;
      }

      .main .content {
        height: 500px;
      }

      .main .content ul {
        display: flex;
        height: 100%;
        flex-direction: column;
        justify-content: space-between;
      }

      .content ul li {
        width: 150px;
        height: 90px;
      }

      .content ul li:hover img {
        opacity: 0;
        transform: translateX(-150px);
      }

      .content li .current {
        opacity: 0;
        transform: translateX(-150px);
      }

      .content ul img {
        width: 100%;
        height: 100%;
        transition: all 0.5s;
      }

html

    <div class="main">
      <div class="box"></div>
      <div class="content">
        <ul>
          <li><img src="./img/5-img/bg0.webp" alt="" /></li>
          <li><img src="./img/5-img/bg1.webp" alt="" /></li>
          <li><img src="./img/5-img/bg2.webp" alt="" /></li>
          <li><img src="./img/5-img/bg3.webp" alt="" /></li>
          <li><img src="./img/5-img/bg4.webp" alt="" /></li>
        </ul>
      </div>
    </div>

javascript

   var list = document.querySelectorAll(".main .content li");
   var img = document.querySelectorAll('img');
   var box = document.querySelector(".main .box");
   var main = document.querySelector(".main");
   var timer = null;
   var num = 1;


   box.style.backgroundImage = "url(./img/5-img/bg0.webp)";    
   function Reset() {
      img.forEach((item,index) => img[index].className = '');
    }

   list.forEach((item, index) => {
     item.addEventListener("mouseenter", function () {
     Reset();
     box.style.backgroundImage = "url(./img/5-img/bg" + index + ".webp)";
     num = index + 1;
        });
      });

    function Auto_Play() {
      timer = setInterval(function () {
          if (num > 4) {
            num = 0;
          }
          Reset();
          img[num].className = 'current';
          box.style.backgroundImage = "url(./img/5-img/bg" + num + ".webp)";    
          num++;
        }, 1600);
      }

      Auto_Play();

    main.addEventListener("mouseenter", function () {
        clearInterval(timer);
      });

    main.addEventListener("mouseleave", function () {
        Auto_Play();
      });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值