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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable