HTML純javaScript代碼寫圖片輪播


<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            .imgbox {
                width: 100%;
                height: 400px;
                position: relative;
                top: 20px;
                left: 15px;
            }
            
            img {
                opacity: 0;
                position: absolute;
                max-height: 400px;
                max-width: 300px;
            }
        </style>
    </head>

    <body>
        <div class="imgbox" id="imgbox">
            <img src="img/19.jpg" style="opacity: 1;" alt="" />
            <img src="img/20.jpg" alt="" />
            <img src="img/21.jpg" alt="" />
            <img src="img/22.jpg" alt="" />
            <img src="img/23.jpg" alt="" />
            <img src="img/24.jpg" alt="" />
        </div>
        <input type="button" value="切換" onclick="change()" />
        <script>
        //全局地址
            var index = 0;

            function change() {
                //得到所 有圖片長度
                var imgs = document.getElementsByTagName("img").length;
                var next = index + 1;
                if (index == imgs - 2) {
                    //imgs-1為長度(從0開始);imgs-2為index(next+1)
                    next = 0;
                }
                //得到所有圖片元素
                var img = document.getElementById("imgbox").children;
                img[index].style.opacity = 0;
                img[next].style.opacity = 1;
                index = next;
                console.log(index);
            }
            window.setInterval("change()", 2000);
        </script>
    </body>

</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM