学习较慢,今天做的轮播图大家一起来评论下吧

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 613px;
            height: 230px;
            margin: 0 auto;
            position: relative;
        }
        .box img{
            width: 100%;
            height: 100%;
            position: absolute;
            top:0;
            left: 0;
        }
        .ctrols{
            width: 112px;
            height: 16px;
            position: absolute;
            bottom: 26px;
            right: 60px;
            z-index: 999;
        }
        .ctrols span{
            display: inline-block;
            border: 2px solid #fff;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 8px;
            background-color: rgb(0,0,0,.2);
        }
        .box img:nth-child(1){
           z-index: 1;
        }
        .ctrols span:nth-child(5){
            margin-right: 0;
        }
        .ctrols span.on{
            background-color: orange;
        }
        .arrow-left ,.arrow-right{
            position: absolute;
            top: 50%;
            width: 30px;
            height: 50px;
            margin-top: -25px;
            background-color:rgb(0,0,0,.2);
            z-index: 10;
            font-size: 30px;
            color: #fff;
            text-align: center;
            line-height: 50px;
        }
        .arrow-left{
            left: 0;
        }
        .arrow-right{
            right: 0;
        }
    </style>
</head>
<body>
    <div class="box">
        <img src="images/img01.jpg"/>//所用图片大家可以随意改变
        <img src="images/img02.jpg"/>
        <img src="images/img03.jpg"/>
        <img src="images/img04.jpg"/>
        <img src="images/img05.jpg"/>
        <div class="ctrols">
            <span class="on"></span><span></span><span></span><span></span><span></span>
        </div>
        <div class="arrow">
            <div class="arrow-left"><</div>
            <div class="arrow-right">></div>
        </div>
    </div>
</body>
</html>

js代码:

<script>
        var spans = document.getElementsByTagName("span");
        var imgs = document.getElementsByTagName("img");
        var arrowLeft = document.getElementsByClassName("arrow-left")[0];
        var arrowRight = document.getElementsByClassName("arrow-right")[0];
        var step = 0;//当前图片的的序号
        //循环绑定span
        for(var i=0; i<spans.length;i++){
            spans[i].idx = i;//在某个span元素上添加一个属性 记住i的值
            spans[i].onclick = function(){
                step = this.idx;
                //取消定时器
                clearInterval(timer);
                changeImg(this.idx);
            }
        }
        var timer = setInterval(function(){
            step++;
            if(step  === 5){
                step = 0;
            }
            changeImg(step);
        },2000);
        // 封装一个图片切换函数
            function changeImg(a){
        // 让图片显示
        // 对应的显示,其他隐藏
        // 先让所有的隐藏
            for(var j=0;j<imgs.length;j++){
                imgs[j].style.display = "none";
                spans[j].className = "";
            }
        // 再让对应的图片显示
               imgs[a].style.display = "block";
        // 当前span设置样式为on
        // 先让所有的span移出on
        // 再设置对应span
               spans[a].className = "on";
        }
        //左单击
        arrowLeft.onclick = function(){
            clearInterval(timer);
            step--;
            if(step<0){
                step = 4;
            }
            changeImg(step);
        }
        //右单击
        arrowRight.onclick = function(){
            clearInterval(timer);
            step++;
            if(step>4){
                step = 0;
            }
            changeImg(step);
        }
    </script>

静态效果:

学习较慢,今天做的轮播图大家一起来评论下吧

 

上一篇:箭头


下一篇:用jQuery实现旋转木马效果的轮播图及解决按钮的闪烁问题