原生js轮播图的实现

1.原生轮播图的自动轮播图

        很多网站都有轮播图,我这里为大家简答的介绍一下,如果有些的不对的地方请大家及时提出意见,也希望给大家带来帮助。

原理:

       将一些图片在一行中平铺,然后通过(css样式去做一个隐藏和默认显示),在用js计算偏移量再利用定时器实现定时轮播。

    <div id="slider">
        <!-- 图片部分 -->
        <ul class="slider-list">
            <!-- <li><a href="#"><img src="./img/5.jpg"></a></li> -->
            <li style="background:#453a94;">1</li>
            <li style="background:#fff1eb;">2</li>
            <li style="background:#4facfe;">3</li>
            <li style="background:#96e6a1;">4</li>
            <li style="background:#fda085;">5</li>
            <li style="background:#a18cd1;">6</li>
        </ul>
        <!-- 小点部分 -->
        <div class="dot-list">
            <span class="dot cur"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
        </div>
        <!--箭头布分 -->
        <a href="#" class="arraw prev" id="left">
            <span></span>
        </a>
        <a href="#" class="arraw next" id="right">
            <span></span>
        </a>

    </div>

ul和li里面是图片,看自己需要几张就写几个li

小圆点部分是那个可以点击切换部分这个是根据图片来设置圆点的个数的不是自己想设置多少

箭头部分是分为左边和右边

2.css样式

  /* 主体部分 */
        
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        #slider {
            position: relative;
            width: 680px;
            height: 340px;
            margin: 100px auto;
            /* outline: 2px solid red; */
            overflow: hidden;
        }
        
        .slider-list {
            display: flex;
            position: relative;
            left: 0px;
            width: 100%;
            height: 100%;
            transition: all 1s;
        }
        
        .slider-list li {
            flex-shrink: 0;
            width: 680px;
            height: 340px;
        }
        
        .dot-list {
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            padding: 2px 10px;
            border-radius: 12px;
            border-color: rgba(255, 255, 255, .3);
        }
        
        .dot-list .dot {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: white;
        }
        
        .dot-list .dot.cur {
            background-color: red;
        }
        
        .arraw {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            width: 30px;
            height: 60px;
            background-color: rgba(0, 0, 0, 0.795);
            display: none;
        }
        
        .prev {
            left: 0;
        }
        
        .next {
            right: 0;
        }
        
        .arraw span {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(50%, 50%);
            width: 10px;
            height: 10px;
            border-bottom: 2px solid white;
            border-left: 2px solid white;
        }
        
        .prev span {
            transform: translate(-50%, -50%) rotate(45deg);
        }
        
        .next span {
            transform: translate(-50%, -50%) rotate(-135deg);
        }
        
        #slider:hover .arraw {
            display: block;
        }

根据html对应css的类名,只做参考

  function lun() {
            index++;
            circe();
            slidLis.style.left = index * -680 + "px";
            slidLis.style.transition = "all 1s";
            if (index === 5) {
                index = 0;
                setTimeout(function() {
                    slidLis.style.left = 0;
                    slidLis.style.transition = "none";
                }, 1000)
            }
            circe();
        }

3.自动轮播的实现

 //  自动轮播
        var autoplay = setInterval(lun, 2000);
        slid.addEventListener("mouseenter", function() {
            clearInterval(autoplay);
        });
        slid.addEventListener("mouseleave", function() {
            clearInterval;
            autoplay = setInterval(lun, 2000);
        })

4.小圆点里面的点击和切换

  //小圆点

        var dotList = document.getElementsByTagName('span');

        function circe() {
            for (var i = 0; i < dotList.length; i++) {
                if (i === index) {
                    dotList[i].classList.add("cur");
                } else {
                    dotList[i].classList.remove("cur");
                }
            }
        }

左边箭头和右边箭头的实现

5.左边和右边点击切换图片

   var index = 0;

        function lun() {
            index++;
            circe();
            slidLis.style.left = index * -680 + "px";
            slidLis.style.transition = "all 1s";
            if (index === 5) {
                index = 0;
                setTimeout(function() {
                    slidLis.style.left = 0;
                    slidLis.style.transition = "none";
                }, 1000)
            }
            circe();
        }
        righ.addEventListener("click", lun);
        lef.addEventListener("click", function() {
            index--;
            if (index === -1) {
                slidLis.style.left = 5 * -680 + "px";
                slidLis.style.transition = "none";
                index = 4;
                setTimeout(function() {
                    slidLis.style.left = index * -680 + "px";
                    slidLis.style.transition = "all 1s";
                }, 0);
            } else {
                slidLis.style.left = index * -680 + "px";
            }
            circe();
        });

  原理就是当点击到小圆点时,得到相应的i值,这个i值也就是span的index值,我们拿他和全局变量index作比较,然后重新设置wrap.style.left的值,然后把i值复制给全局变量index,最后显示当前的小原点即可。值得注意的是:这里涉及到了闭包的概念,如果直接使用for循环,则不能得到正确的结果。

  最终效果如图:

展示一下代码:

<!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>js无缝轮播</title>
    <style>
        /* 主体部分 */
        
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        #slider {
            position: relative;
            width: 680px;
            height: 340px;
            margin: 100px auto;
            /* outline: 2px solid red; */
            overflow: hidden;
        }
        
        .slider-list {
            display: flex;
            position: relative;
            left: 0px;
            width: 100%;
            height: 100%;
            transition: all 1s;
        }
        
        .slider-list li {
            flex-shrink: 0;
            width: 680px;
            height: 340px;
        }
        
        .dot-list {
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            padding: 2px 10px;
            border-radius: 12px;
            border-color: rgba(255, 255, 255, .3);
        }
        
        .dot-list .dot {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: white;
        }
        
        .dot-list .dot.cur {
            background-color: red;
        }
        
        .arraw {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            width: 30px;
            height: 60px;
            background-color: rgba(0, 0, 0, 0.795);
            display: none;
        }
        
        .prev {
            left: 0;
        }
        
        .next {
            right: 0;
        }
        
        .arraw span {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(50%, 50%);
            width: 10px;
            height: 10px;
            border-bottom: 2px solid white;
            border-left: 2px solid white;
        }
        
        .prev span {
            transform: translate(-50%, -50%) rotate(45deg);
        }
        
        .next span {
            transform: translate(-50%, -50%) rotate(-135deg);
        }
        
        #slider:hover .arraw {
            display: block;
        }
    </style>
</head>

<body>
    <div id="slider">
        <!-- 图片部分 -->
        <ul class="slider-list">
            <!-- <li><a href="#"><img src="./img/5.jpg"></a></li> -->
            <li style="background:#453a94;">1</li>
            <li style="background:#fff1eb;">2</li>
            <li style="background:#4facfe;">3</li>
            <li style="background:#96e6a1;">4</li>
            <li style="background:#fda085;">5</li>
            <li style="background:#a18cd1;">6</li>
        </ul>
        <!-- 小点部分 -->
        <div class="dot-list">
            <span class="dot cur"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
        </div>
        <!--箭头布分 -->
        <a href="#" class="arraw prev" id="left">
            <span></span>
        </a>
        <a href="#" class="arraw next" id="right">
            <span></span>
        </a>

    </div>
    <script>
        var slid = document.getElementById('slider');
        var slidLis = slid.getElementsByTagName('ul')[0];
    
        var slidLi = slidLis.getElementsByTagName('li');

        var lef = document.getElementById('left');
        var righ = document.getElementById('right');

        var index = 0;

        function lun() {
            index++;
            circe();
            slidLis.style.left = index * -680 + "px";
            slidLis.style.transition = "all 1s";
            if (index === 5) {
                index = 0;
                setTimeout(function() {
                    slidLis.style.left = 0;
                    slidLis.style.transition = "none";
                }, 1000)
            }
            circe();
        }
        righ.addEventListener("click", lun);
        lef.addEventListener("click", function() {
            index--;
            if (index === -1) {
                slidLis.style.left = 5 * -680 + "px";
                slidLis.style.transition = "none";
                index = 4;
                setTimeout(function() {
                    slidLis.style.left = index * -680 + "px";
                    slidLis.style.transition = "all 1s";
                }, 0);
            } else {
                slidLis.style.left = index * -680 + "px";
            }
            circe();
        });
        //  自动轮播
        var autoplay = setInterval(lun, 2000);
        slid.addEventListener("mouseenter", function() {
            clearInterval(autoplay);
        });
        slid.addEventListener("mouseleave", function() {
            clearInterval;
            autoplay = setInterval(lun, 2000);
        })

        //小圆点

        var dotList = document.getElementsByTagName('span');

        function circe() {
            for (var i = 0; i < dotList.length; i++) {
                if (i === index) {
                    dotList[i].classList.add("cur");
                } else {
                    dotList[i].classList.remove("cur");
                }
            }
        }
    </script>
</body>

</html>

  • 2
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页
评论

打赏作者

框框字

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值