mxs___ 2021-08-30 22:56 采纳率: 78.6%
浏览 49
已结题

js轮播图不能实现缓慢往左滑动的效果

js轮播图出不来效果,想实现那种慢慢往左滑动的效果,不是那种突然就换图片那种,是不是定时器里边再套一个定时器,有没有大牛帮我改一下,感激不尽!


<!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-type: none;
        }
        .box {
            position: relative;
            width: 600px;
            height: 400px;
            margin: 100px auto;
            border: 1px solid #000;
            overflow: hidden;
        }
        .box ul {
            position: absolute;
            width: 2400px;
        }
      .box li {
           float: left;
           font-size: 0;
       }
       .circle {
           position: absolute;
           bottom: 10px;
           left: 0;
           width: 100%;
           font-size: 0;
           text-align: center;
          
       }
       .circle span {
           display: inline-block;
            width: 20px;
            height: 20px;
            margin: 2px;
            border-radius: 100%;
            background-color: rgba(0,0,0,.5);
       }
       .box button {
           position: absolute;
           width: 40px;
           height: 50px;
           top: 50%;
           margin-top: -25px;
            font-size: 30px;
            color: #fff;
            border: 0;

            background-color: rgba(0,0,0,.5);
       }
       button:nth-of-type(1) {
           left: 0;
       }
       button:nth-of-type(2) {
           right: 0;
       }
       span.active {
           background-color: rgba(255,0,0,.5);
       }
      
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <li><img src="./img/t1.png" alt=""></li>
            <li><img src="./img/t2.png" alt=""></li>
            <li><img src="./img/t3.png" alt=""></li>
            <li><img src="./img/t4.png" alt=""></li>
            <li><img src="./img/t1.png" alt=""></li>
        </ul>
        <div class="circle">
            <span class="active"></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
        <button>&lt;</button> <button>&gt;</button>
    </div>

    <script>
        var btn = document.querySelectorAll('button');
        var ul = document.querySelector('ul');
        var span = document.querySelectorAll('span');
        var bw =parseInt(getComputedStyle(document.querySelector('.box')).width) ;
        console.log(bw);
        var n = 0 ;
        setInterval(function () { 
            n++;
            if (n == 4) {
               n = 0;
           }
          
         t=  setInterval(function () {
               var l = parseInt(getComputedStyle(ul).left) - 20;
               if (l <= n*-bw) {
                   l=n*-bw
                   clearInterval(t);
               }
               ul.style.left = l + 'px';
           }, 70);
        }, 2000);
    </script>
</body>

</html>
  • 写回答

2条回答 默认 最新

  • limit、T 2021-08-31 09:39
    关注

    我认为你需要css的动画效果。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月12日
  • 已采纳回答 9月4日
  • 创建了问题 8月30日

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失