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

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条回答 默认 最新

  • TheWaSaiBoy 2021-08-31 09:39
    已采纳

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

    打赏 评论
  • 前行的枫 2021-08-31 09:41

    主要用transform来实现

    1 打赏 评论

相关推荐 更多相似问题