JS循环轮播图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>1022数组</title>
    <style>
        #container{
            position: relative;
            height: 400px;
            width: 1240px;
            margin: 50px auto;
        }
        .img{
            position: absolute;
            width: 450px;
            height: 300px;
            top: 50px;
            left: 395px;
            bottom: 0;
            z-index: 0;
            background-color: #404040;
            font-size: 48px;
            color: #fff;
            transition: all 0.8s ease-out;
        }
        .img:nth-of-type(1){
            width: 450px;
            height: 300px;
            top: 50px;
            left: 0;
            z-index: 1;
            opacity: 0.8;
        }
        .img:nth-of-type(2){
            width: 525px;
            height: 350px;
            top: 25px;
            left: 140px;
            z-index: 2;
            opacity: 0.9;
        }
        .img:nth-of-type(3){
            width: 600px;
            height: 400px;
            top: 0;
            left: 320px;
            z-index: 3;
            opacity: 1;
        }
        .img:nth-of-type(4){
            width: 525px;
            height: 350px;
            top: 25px;
            left: 575px;
            z-index: 2;
            opacity: 0.9;

        }
        .img:nth-of-type(5){
            width: 450px;
            height: 300px;
            top: 50px;
            left: 790px;
            z-index: 1;
            opacity: 0.8;
        }
    </style>
</head>
<body>
<div id="container">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
    <img src="" alt="" class="img">
</div>

</body>
<script>
    var imgs = document.getElementsByClassName("img");
    var container = document.getElementById("container");
    var imgSrc = [" http://cdn.attach.qdfuns.com/notes/pics/201710/26/120712ngt2edra2crr7dgd.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120652w2q5xqqr8lt9ry7r.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120714s2o8oolp628lhnhb.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120553pqqp08q6pkkqpq8o.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120641af8ja5noa88joc8n.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120644mwoeh09r9rzwxzn8.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120725h4jskiflsivw15k1.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120635qga4ljgurljnzrvj.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120728nn78gfnyinf2gf7i.jpg", " http://cdn.attach.qdfuns.com/notes/pics/201710/26/120632lpjom8ppsl26dd68.jpg"];
    var numA = 2;
    var numB = 0;


    for (var i = 0; i < imgs.length; i ++) {
        imgs[i].src = imgSrc[i];
        imgs[i].index = i;//给每张图片添加一个序号,通过这个序号来计算套用哪个样式
    }


    for ( var  k = 0; k < imgs.length; k ++) {
        imgs[k].onclick = function () {

            //通过激活序号(numA)和当前按钮序号(numB)的差算出新的序号,用for来更新一遍
            numB = this.index;
            for (var i = 0; i < imgs.length; i ++) {
                if (imgs[i].index + numA - numB >= 0) {
                    imgs[i].index = (imgs[i].index + numA - numB) % imgs.length;
                } else if (imgs[i].index + numA - numB < 0) {
                    imgs[i].index = imgs[i].index + numA - numB + imgs.length;
                }

                //根据序号刷新样式
                if (imgs[i].index === 0) {
                    imgs[i].style.width = 450 + "px";
                    imgs[i].style.height = 300 + "px";
                    imgs[i].style.zIndex = 1;
                    imgs[i].style.top = 50 + "px";
                    imgs[i].style.left = 0 + "px";
                    imgs[i].style.opacity = 0.5
                } else if (imgs[i].index === 1) {
                    imgs[i].style.width = 525 + "px";
                    imgs[i].style.height = 350 + "px";
                    imgs[i].style.zIndex = 2;
                    imgs[i].style.top = 25 + "px";
                    imgs[i].style.left = 140 + "px";
                    imgs[i].style.opacity = 0.8
                } else if (imgs[i].index === 2) {
                    imgs[i].style.width = 600 + "px";
                    imgs[i].style.height = 400 + "px";
                    imgs[i].style.top = 0 + "px";
                    imgs[i].style.zIndex = 3;
                    imgs[i].style.left = 320 + "px";
                    imgs[i].style.opacity = 1
                } else if (imgs[i].index === 3) {
                    imgs[i].style.width = 525 + "px";
                    imgs[i].style.height = 350 + "px";
                    imgs[i].style.zIndex = 2;
                    imgs[i].style.top = 25 + "px";
                    imgs[i].style.left = 575 + "px";
                    imgs[i].style.opacity = 0.8
                } else if (imgs[i].index === 4) {
                    imgs[i].style.width = 450 + "px";
                    imgs[i].style.height = 300 + "px";
                    imgs[i].style.zIndex = 1;
                    imgs[i].style.top = 50 + "px";
                    imgs[i].style.left = 790 + "px";
                    imgs[i].style.opacity = 0.5
                } else {
                    imgs[i].style.width = 450 + "px";
                    imgs[i].style.height = 300 + "px";
                    imgs[i].style.zIndex = 0;
                    imgs[i].style.top = 50 + "px";
                    imgs[i].style.left = 395 + "px";
                    imgs[i].style.opacity = 0
                }
            }
        };

    }

    //定时器部分
    function play() {
        //自动播放相当于点击序号2的图片
        for (var j = 0; j < imgs.length; j ++) {
            if (imgs[j].index === 1) {
                imgs[j].onclick();
                break  //这个break不能省,不然会有bug
            }
        }
    }
    var imgPlay = window.setInterval(play,2000);
    container.onmouseover = function () {
        window.clearInterval(imgPlay);
    };
    container.onmouseout = function () {
        imgPlay = window.setInterval(play,2000);
    };





</script>
</html>

资料进群领取598399936
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 jQuery 的循环的代码: ```html <div class="slideshow-container"> <div class="mySlides fade"> <img src="img1.jpg"> </div> <div class="mySlides fade"> <img src="img2.jpg"> </div> <div class="mySlides fade"> <img src="img3.jpg"> </div> <a class="prev">❮</a> <a class="next">❯</a> </div> <br> <div style="text-align:center"> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> </div> ``` ```css /* 轮容器 */ .slideshow-container { max-width: 1000px; position: relative; margin: auto; } /* 隐藏所有幻灯片 */ .mySlides { display: none; } /* 下一张和上一张控件 */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -22px; padding: 16px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; } /* 下一张控件的位置 */ .next { right: 0; border-radius: 3px 0 0 3px; } /* 当用户鼠标悬停在控件上时,改变背景颜色 */ .prev:hover, .next:hover { background-color: rgba(0,0,0,0.8); } /* 控制圆点 */ .dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } /* 将当前圆点更改为未选中状态 */ .active, .dot:hover { background-color: #717171; } /* 淡入幻灯片 */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from {opacity: .4} to {opacity: 1} } @keyframes fade { from {opacity: .4} to {opacity: 1} } ``` JavaScript 代码可以用来控制幻灯片的滑动: ```javascript var slideIndex = 0; showSlides(); function showSlides() { var i; var slides = $(".mySlides"); var dots = $(".dot"); for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } slideIndex++; if (slideIndex > slides.length) {slideIndex = 1} for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex-1].style.display = "block"; dots[slideIndex-1].className += " active"; setTimeout(showSlides, 2000); // 每 2 秒切换一张幻灯片 } $(".prev").click(function() { slideIndex--; if (slideIndex < 1) {slideIndex = $(".mySlides").length} showSlides(); }); $(".next").click(function() { slideIndex++; if (slideIndex > $(".mySlides").length) {slideIndex = 1} showSlides(); }); $(".dot").click(function() { slideIndex = $(this).index() + 1; showSlides(); }); ``` 以上是一个简单的循环的代码,你可以根据自己的需求进行修改。在这个代码中,幻灯片会每 2 秒自动切换一次,并且有“上一张”和“下一张”按钮,以及圆点控制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值