zoukankan      html  css  js  c++  java
  • 小白之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;376px;"   alt="">
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="2.jpg" style="height:282px;376px;"   alt="">
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="3.jpg" style="height:282px;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;376px;}
            #scroll{list-style: none;margin:0;padding:0;}
            #scroll>li{display: none}
            #btns{376px;position:absolute;bottom:0;text-align: center;padding:5px 0;}
            #btns span{background:#f5f5f5;display: inline-block;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;376px;}
            #scroll{list-style: none;margin:0;padding:0;}
            #scroll>li{display: none}
            #btns{376px;position:absolute;bottom:0;text-align: center;padding:5px 0;}
            #btns span{background:#f5f5f5;display: inline-block;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;376px;"   alt="">
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="2.jpg" style="height:282px;376px;"   alt="">
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="3.jpg" style="height:282px;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>
    

      

  • 相关阅读:
    团队项目的NABC(截图软件)
    《梦断代码》读后感_3
    《梦断代码》读后感_2
    毕设今日总结(二)
    毕业设计今日总结(一)
    QT中文乱码解决方法
    课堂练习——最大联通之数组
    《浪潮之巅》读书笔记3
    《浪潮之巅》读书笔记2
    《浪潮之巅》读书笔记1
  • 原文地址:https://www.cnblogs.com/s313139232/p/7508618.html
Copyright © 2011-2022 走看看