實用的原生js圖片輪播

來源:互聯網
上載者:User

標籤:use   tle   hid   attr   圖片輪播   java   執行   div   自訂屬性   

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
        }      
        body {
            padding: 20px;
        }
        
        #container {
            position: relative;
            width: 600px;
            height: 400px;
            border: 3px solid #333;
            overflow: hidden;
        }  
        #list {
            position: absolute;
            z-index: 1;
            width: 4200px;
            height: 400px;
        }
        
        #list img {
            float: left;
            width: 600px;
            height: 400px;
        }
        
        #buttons {
            position: absolute;
            left: 250px;
            bottom: 20px;
            z-index: 2;
            height: 10px;
            width: 100px;
        }
        
        #buttons span {
            float: left;
            margin-right: 5px;
            width: 10px;
            height: 10px;
            border: 1px solid #fff;
            border-radius: 50%;
            background: #333;
            cursor: pointer;
        }
        
        #buttons .on {
            background: orangered;
        }
        
        .arrow {
            position: absolute;
            top: 180px;
            z-index: 2;
            display: none;
            width: 40px;
            height: 40px;
            font-size: 36px;
            font-weight: bold;
            line-height: 39px;
            text-align: center;
            color: #fff;
            background-color: RGBA(0, 0, 0, .3);
            cursor: pointer;
        }
        
        .arrow:hover {
            background-color: RGBA(0, 0, 0, .7);
        }
        
        #container:hover .arrow {
            display: block;
        }
        
        #prev {
            left: 20px;
        }
        
        #next {
            right: 20px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var container = document.getElementById(‘container‘);
            var list = document.getElementById(‘list‘);
            var buttons = document.getElementById(‘buttons‘).getElementsByTagName(‘span‘);
            var prev = document.getElementById(‘prev‘);
            var next = document.getElementById(‘next‘);
            var index = 1;
            var timer;

            function animate(offset) {
                //擷取的是style.left,是相對左邊擷取距離,所以第一張圖後style.left都為負值,
                //且style.left擷取的是字串,需要用parseInt()取整轉化為數字。
                var newLeft = parseInt(list.style.left) + offset;
                list.style.left = newLeft + ‘px‘;
                //無限滾動判斷
                if (newLeft > -600) {
                    list.style.left = -3000 + ‘px‘;
                }
                if (newLeft < -3000) {
                    list.style.left = -600 + ‘px‘;
                }
            }
            function play() {
                //重複執行的定時器
                timer = setInterval(function() {
                    next.onclick();
                }, 2000)
            }

            function stop() {
                clearInterval(timer);
            }

            function buttonsShow() {
                //將之前的小圓點的樣式清除
                for (var i = 0; i < buttons.length; i++) {
                    if (buttons[i].className == "on") {
                        buttons[i].className = "";
                    }
                }
                //數組從0開始,故index需要-1
                buttons[index - 1].className = "on";
            }
            prev.onclick = function() {
                index -= 1;
                if (index < 1) {
                    index = 5
                }
                buttonsShow();
                animate(600);
            };
            next.onclick = function() {
                //由於上邊定時器的作用,index會一直遞增下去,我們只有5個小圓點,所以需要做出判斷
                index += 1;
                if (index > 5) {
                    index = 1
                }
                animate(-600);
                buttonsShow();
            };

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

                        /*   這裡獲得滑鼠移動到小圓點的位置,用this把index綁定到對象buttons[i]上,去Googlethis的用法  */
                        /*   由於這裡的index是自訂屬性,需要用到getAttribute()這個DOM2級方法,去擷取自訂index的屬性*/
                        var clickIndex = parseInt(this.getAttribute(‘index‘));
                        var offset = 600 * (index - clickIndex); //這個index是當前圖片停留時的index
                        animate(offset);
                        index = clickIndex; //存放滑鼠點擊後的位置,用於小圓點的正常顯示
                        buttonsShow();
                    }
                })(i)
            }
            container.onmouseover = stop;
            container.onmouseout = play;
            play();
        }
    </script>
</head>
<body>
    <div id="container">
        <div id="list" style="left: -600px;">
            <img src="http://images2015.cnblogs.com/blog/32818/201705/32818-20170527142827341-239546623.jpg" alt="1" />
            <img src="http://images2015.cnblogs.com/blog/32818/201705/32818-20170527142827341-239546623.jpg" alt="1" />
            <img src="http://img.netbian.com/file/2017/0717/9bba6a121f15845698285e7ab3bbda13.jpg" alt="2" />
            <img src="http://img.netbian.com/file/2017/0724/bb823c2d94eb4a7b17920ed8d4458c23.jpg" alt="3" />
            <img src="http://img.netbian.com/file/2017/0724/93e1f7585986374bf6903e4f95c4fa93.jpg" alt="4" />
            <img src="http://img.netbian.com/file/2017/0718/1100e085b03b989d6c2691e7224485af.jpg" alt="5" />
            <img src="http://img.netbian.com/file/2017/0724/d8ed03e528840690fdfe89cacba8eb92.jpg" alt="5" />
        </div>
        <div id="buttons">
            <span index="1" class="on"></span>
            <span index="2"></span>
            <span index="3"></span>
            <span index="4"></span>
            <span index="5"></span>
        </div>
        <a href="javascript:;" id="prev" class="arrow">&lt;</a>
        <a href="javascript:;" id="next" class="arrow">&gt;</a>
    </div>

</body>

</html>

實用的原生js圖片輪播

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.