原生JS 缓动轮播图

☞ 为了更直观 没有隐藏其他图片
在这里插入图片描述
在这里插入图片描述

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        li {
            list-style-type: none;
        }
        
        .nav {
            width: 480px;
            height: 200px;
            background-color: darkgoldenrod;
            margin: 200px auto;
            position: relative;
        }
        
        .uls {
            width: 700%;
            height: 200px;
            position: absolute;
            top: 0;
            left: 0;
        }
        
        .uls li {
            float: left;
        }
        
        img {
            width: 480px;
            height: 200px;
        }
        
        ol {
            width: 110px;
            height: 20px;
            position: absolute;
            bottom: 10px;
            border-radius: 10px;
            display: flex;
            justify-content: space-around;
            align-items: center;
            left: 50%;
            transform: translateX(-50%);
            background-color: rgba(78, 88, 78, .57);
        }
        
        ol li {
            width: 6px;
            height: 6px;
            border-radius: 2px;
            background-color: rgb(231, 230, 243);
        }
        
        .right {
            right: 0;
        }
        
        .left,
        .right {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            z-index: 1;
            width: 30px;
            height: 70px;
            color: #fff;
            text-decoration: none;
            text-align: center;
            line-height: 70px;
            background-color: rgba(114, 114, 114, 0.582);
        }
        
        .col {
            width: 10px;
            background-color: darkturquoise;
        }
    </style>
</head>

<body>
    <div class="nav">
        <a href="javascript:;" class="left">
            <</a>
                <a href="javascript:;" class="right">></a>
                <ul class="uls">
                    <li><img src="images/1.jpg" alt=""></li>
                    <li><img src="images/2.jpg" alt=""></li>
                    <li><img src="images/3.jpg" alt=""></li>
                    <li><img src="images/4.jpg" alt=""></li>
                    <li><img src="images/5.jpg" alt=""></li>
                    <li><img src="images/6.jpg" alt=""></li>
                </ul>
                <ol class="ols">
                    <li class="col"></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                </ol>
    </div>
</body>


<script>
    var nav = document.querySelector('.nav');
    var ul = document.querySelector('.uls');
    var ol = document.querySelector('.ols');
    var la = document.querySelector('.left');
    var ra = document.querySelector('.right');
    // 
    var navWidth = nav.offsetWidth;
    var timer = null;
    var i = 0;
    var y = 0;
    // 动画函数
    function ainmate(obj, tag) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function() {
            step = (tag - obj.offsetLeft) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (obj.offsetLeft == tag) {
                clearInterval(obj.timer);
            }
            obj.style.left = obj.offsetLeft + step + 'px';
        }, 20)
    }

    // 圆点
    for (var k = 0; k < ol.children.length; k++) {
        ol.children[k].index = k;
        ol.children[k].onclick = function() {
            for (var k = 0; k < ol.children.length; k++) {
                ol.children[k].className = '';
            }
            this.className = 'col';
            var index = this.index;
            ainmate(ul, -index * navWidth);
            // 赋值  解决圆点点击后和图片不同步的BUG
            i = index;
            y = index;
        }
    }
    // 克隆
    var flise = ul.children[0].cloneNode(true);
    ul.appendChild(flise);
    // 按钮
    // 右按钮
    ra.onclick = function() {
        if (i == 6) {
            ul.style.left = 0;
            i = 0;
        }
        i++;
        ainmate(ul, -i * navWidth);
        // 圆点跟随
        for (var j = 0; j < ol.children.length; j++) {
            ol.children[j].className = '';
        }
        y++;
        if (y == 6) y = 0;
        ol.children[y].className = 'col';
    }

    // 左按钮
    la.onclick = function() {
        if (i == 0) {
            ul.style.left = -6 * navWidth + 'px';
            i = 6;
        }
        i--;
        ainmate(ul, -i * navWidth);
        // 圆点跟随
        for (var j = 0; j < ol.children.length; j++) {
            ol.children[j].className = '';
        }
        y--;
        if (y == 0) y = 6;
        ol.children[i].className = 'col';
    }


    // 自动播放
    var timers = setInterval(function() {
        // 手动调用点击事件
        ra.click();
    }, 2000)

    // 
    // 移入暂停
    nav.onmouseover = function() {
        clearInterval(timers);
    }

    // 移除继续
    nav.onmouseout = function() {
        timers = setInterval(function() {
            // 手动调用点击事件
            ra.click();
        }, 2000)
    }
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值