1. 程式人生 > >原生javascript寫一個精美的輪播圖

原生javascript寫一個精美的輪播圖

css樣式
* {
padding: 0;
margin: 0;
list-style: none;
text-decoration: none;
}

        #box {
            width: 400px;
            height: 250px;
            border: 1px solid firebrick;
            margin: 100px auto;
            position: relative;
            overflow: hidden;
            cursor: pointer;
        }

        ul {
            position: absolute;
            width: 500%;
            height: 250px;
            transition: 0.5s;
        }

        ul li {
            float: left;
            width: 400px;
            height: 250px;
        }

        ul li img {
            width: 400px;
            height: 250px;
        }

        #nav {
            width: 200px;
            height: 40px;
            position: absolute;
            bottom: 0px;
            left: 130px;
        }

        #nav span {
            width: 20px;
            height: 20px;
            background: green;
            display: inline-block;
            border-radius: 50px;
            text-align: center;
            line-height: 20px;
            font-size: 12px;
            cursor: pointer;
        }

        #nav .active {
            background: gold;
            color:#FF0000;
        }

        .left,.right {
            width: 20px;
            height: 30px;
            background: black;
            position: absolute;
            top: calc(50% - 15px);
            opacity: 0.5;
            color: white;
            font-size: 20px;
            line-height: 30px;
            text-align: center;
            cursor: pointer;
        }           
        .right {
            right: 0;
        }

html程式碼

 <div id="box">
        <ul>
            <li><img src="imgs/1.jpg" /></li>
            <li><img src="imgs/2.jpg" /></li>
            <li><img src="imgs/3.jpg" /></li>
            <li><img src="imgs/4.jpg" /></li>
            <li><img src="imgs/5.jpg" /></li>
        </ul>
        <div id="nav">
            <span class="active">1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
        </div>
        <div class="btn">
            <div class="left"><</div>                   
            <div class="right">></div>              
        </div>


js程式碼

            var oBox = document.getElementById('box');
            var oUl = oBox.getElementsByTagName('ul')[0];
            var oLi = oUl.getElementsByTagName('li')[0];
            var oNav = document.getElementById('nav');
            var oSpan = oNav.getElementsByTagName('span');
            var oBtn = document.querySelectorAll('.btn')[0];
            var oDiv = oBtn.getElementsByTagName('div');
            //console.log(oDiv)                                                                     
            // 六個點中,點選當前的這個點時,顏色變為active;
            var index = 0; //儲存當前小圓點的樣式
            var num = 0; //給按鈕設定變數
            var timer = null;
            play();//呼叫
            for(var i = 0; i < oSpan.length; i++){
                oSpan[i].index = i; //導航原點的下標等於當前的圖片
                oSpan[i].onclick = function(){                          
                    oSpan[index].className = ''; //清除前面一個遠點的樣式   
                    index = this.index;//這個下標等於當前oSpan的下標
                    this.className = 'active';
                    oUl.style.marginLeft = -400 * index + 'px';                                              
                }
            }
            for(var i = 0; i < oDiv.length; i++){     //按鈕迴圈
                oDiv[i].index = i;
                oDiv[i].onclick = function(){   
                    oSpan[index].className = '';      //在點選後清空前面的小原點的樣式             
                    num = this.index;      //指的是左右按鈕的下標0 ,1.                
                    if(num){         // 這裡要麼是0,要麼是1 num是左右按鈕                        
                        index ++;
                        if(index > 4){index = 0}
                    }else{
                        index --;
                        if(index < 0){index = 4}
                    }
                    // 這裡要將this換成oSpan[index],在這裡this指向oDiv
                    oSpan[index].className = 'active';
                    oUl.style.marginLeft = -400 * index + 'px';                                             
                }
            }
            //設定定時器
            function play(){
                timer = setInterval(function(){
                    oSpan[index].className = '';    
                    index ++;
                    if(index > 4){index = 0}
                    oSpan[index].className = 'active';
                    oUl.style.marginLeft = -400 * index + 'px';   
                },2000)                 
            };
            //清除定時器
            oBox.onmouseover = function(){
                clearInterval(timer);
            };
            oBox.onmouseout = function(){
                play(); //程式碼重複 用play()替代