小白之js原生轮播图

小白之js原生轮播图

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="banner.css">
    <script src="banner.js"></script>
</head>
<body>
<div class="ban">
    <ul id="scroll">
        <li>
            <a href="#">
                <img src="1.jpg" style="height:282px;width:376px;"   alt="">
            </a>
        </li>
        <li>
            <a href="#">
                <img src="2.jpg" style="height:282px;width:376px;"   alt="">
            </a>
        </li>
        <li>
            <a href="#">
                <img src="3.jpg" style="height:282px;width:376px;"   alt="">
            </a>
        </li>
    </ul>
<ul id="btns">
    <span class="active"></span>
    <span></span>
    <span></span>
</ul>
<button id="shang"><</button>
<button id="xia">></button>
</div>
</body>
</html>

css:(banner.css)

.ban{position:relative;height:282px;width:376px;}
        #scroll{list-style: none;margin:0;padding:0;}
        #scroll>li{display: none}
        #btns{width:376px;position:absolute;bottom:0;text-align: center;padding:5px 0;}
        #btns span{background:#f5f5f5;display: inline-block;width:10px;height:10px;border-radius: 50%;}
        .ban .active{background:#555!important;}
        #shang,#xia{background: rgba(255,255,255,0.7);padding:10px;border: 0;position: absolute;top:130px;z-index: 100000;color:#000;font-weight: bold}
        #shang{left:0}
        #xia{right:0}

js:(banner.js)

  var scroll = document.getElementById("scroll");
        var scrollli = document.getElementById("scroll").getElementsByTagName("li");
        var btns = document.getElementById("btns").getElementsByTagName("span");
        var shangbtn = document.getElementById("shang");
        var xiabtn = document.getElementById("xia");
        var index = 1;

        scrollli[0].style.display = "block";

        function clearimg(){
            /*把所有图片隐藏*/
            for(var i=0;i<scrollli.length;i++){
                scrollli[i].style.display="none";
            }
        }
        function clearactive(){
            for(var i=0;i<btns.length;i++){
                btns[i].classList.remove("active");
            }
        }

        xiabtn.onclick=function() {
            clearimg();clearactive();
            index++;
            if(index>3){
                index=1;
            }
            /*把需要显示的图片显示出来*/
            scrollli[index-1].style.display="block";
            btns[index-1].classList.add("active");
        };

        shangbtn.onclick=function() {
            clearimg();clearactive();
            index--;
            if(index<1){
                index=3
            }
            /*把需要显示的图片显示出来*/
            scrollli[index-1].style.display="block";
            btns[index-1].classList.add("active");
        };
        function btnsclick(y) {
            clearimg();
            clearactive();
            console.log(y);
            /*把需要显示的图片显示出来*/
            console.log(scrollli[0])
            scrollli[y].style.display = "block";
            btns[y].classList.add("active");
            index = y+1;
        }
        for(var i=0; i<btns.length; i++){
            (function(j){
                btns[j].onclick = function () {
                    btnsclick(j)
                };
            })(i)
        }

        setInterval(function () {
            clearimg();
            clearactive();
            index++;
            if(index>3){
                index=1;
            }
            /*把需要显示的图片显示出来*/
            scrollli[index-1].style.display="block";
            btns[index-1].classList.add("active");
        },3000);

单文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .ban{position:relative;height:282px;width:376px;}
        #scroll{list-style: none;margin:0;padding:0;}
        #scroll>li{display: none}
        #btns{width:376px;position:absolute;bottom:0;text-align: center;padding:5px 0;}
        #btns span{background:#f5f5f5;display: inline-block;width:10px;height:10px;border-radius: 50%;}
        .ban .active{background:#555!important;}
        #shang,#xia{background: rgba(255,255,255,0.7);padding:10px;border: 0;position: absolute;top:130px;z-index: 100000;color:#000;font-weight: bold}
        #shang{left:0}
        #xia{right:0}
    </style>
</head>
<body>
<div class="ban">
    <ul id="scroll">
        <li>
            <a href="#">
                <img src="1.jpg" style="height:282px;width:376px;"   alt="">
            </a>
        </li>
        <li>
            <a href="#">
                <img src="2.jpg" style="height:282px;width:376px;"   alt="">
            </a>
        </li>
        <li>
            <a href="#">
                <img src="3.jpg" style="height:282px;width:376px;"   alt="">
            </a>
        </li>
    </ul>
<ul id="btns">
    <span class="active"></span>
    <span></span>
    <span></span>
</ul>
<button id="shang"><</button>
<button id="xia">></button>
</div>
<script>
        var scroll = document.getElementById("scroll");
        var scrollli = document.getElementById("scroll").getElementsByTagName("li");
        var btns = document.getElementById("btns").getElementsByTagName("span");
        var shangbtn = document.getElementById("shang");
        var xiabtn = document.getElementById("xia");
        var index = 1;

        scrollli[0].style.display = "block";

        function clearimg(){
            /*把所有图片隐藏*/
            for(var i=0;i<scrollli.length;i++){
                scrollli[i].style.display="none";
            }
        }
        function clearactive(){
            for(var i=0;i<btns.length;i++){
                btns[i].classList.remove("active");
            }
        }

        xiabtn.onclick=function() {
            clearimg();clearactive();
            index++;
            if(index>3){
                index=1;
            }
            /*把需要显示的图片显示出来*/
            scrollli[index-1].style.display="block";
            btns[index-1].classList.add("active");
        };

        shangbtn.onclick=function() {
            clearimg();clearactive();
            index--;
            if(index<1){
                index=3
            }
            /*把需要显示的图片显示出来*/
            scrollli[index-1].style.display="block";
            btns[index-1].classList.add("active");
        };
        function btnsclick(y) {
            clearimg();
            clearactive();
            console.log(y);
            /*把需要显示的图片显示出来*/
            console.log(scrollli[0])
            scrollli[y].style.display = "block";
            btns[y].classList.add("active");
            index = y+1;
        }
        for(var i=0; i<btns.length; i++){
            (function(j){
                btns[j].onclick = function () {
                    btnsclick(j)
                };
            })(i)
        }

        setInterval(function () {
            clearimg();
            clearactive();
            index++;
            if(index>3){
                index=1;
            }
            /*把需要显示的图片显示出来*/
            scrollli[index-1].style.display="block";
            btns[index-1].classList.add("active");
        },3000);
</script>
</body>
</html>

  

posted @ 2017-09-12 09:58  莫小龙  阅读(164)  评论(0编辑  收藏  举报