原生 js 左右切換輪播圖

來源:互聯網
上載者:User

標籤:

使用方法:
可能很多人對輪播圖感興趣,下面奉上本人的 原生 js 輪播代碼
複製 js 到頁面的最底部,樣式在 css 裡改,js 基本不用動,有什麼不懂的可以 加本人 QQ172360937 諮詢 或者留言都可以,這個是用 原生寫的 輪播圖,樣式可以寫自己喜歡的樣式,什麼都不用改,只改變樣式就行,頁面結構的id 要與js的相對應li隨便加。li 隨便加的意思就是說你可以加無數個圖片。每個li 裡裝一個圖片,或者是其他什麼元素,

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        *{padding:0;margin:0;}        div{            width:200px;/*必須有*/            height:50px;/*必須有*/            position:relative;/*必須有===*/            overflow:hidden;/*必須有===*/            -webkit-user-select: none;/*必須有*/            -moz-user-select: none;/*必須有*/            -ms-user-select: none;/*必須有*/            user-select: none;/*必須有*/            margin:20px;        }        ul{            position:absolute;/*必須有*/            left:0;/*必須有*/            font-size:0;/*必須有*/        }        li{list-style: none;/*必須有*/            font-size:16px;/*必須有*/            height:50px;/*必須有*/            width:200px;/*必須有*/            float:left;/*必須有*/        }        #parent{            width:50%;            height:200px;            border:1px solid red;            margin:0 auto;        }        .span{            display:inline-block;/*必須有*/            width:20px;            height:20px;            line-height:20px;            text-align: center;            border-radius:50%;            color:white;            cursor:pointer;/*必須有*/        }    </style></head><body><div id="parent">    <div>        <ul id=‘ul‘>            <li style=‘background:red;‘>第一個</li>            <li style=‘background:yellow;‘>第二個</li>            <li style=‘background:pink;‘>第三個</li>            <li style=‘background:green;‘>第四個</li>            <li style=‘background:blue;‘>第五個</li>            <li style=‘background:blue;‘>第6個</li>            <li style=‘background:green;‘>第7個</li>            <li style=‘background:blue;‘>第8個</li>            <li style=‘background:green;‘>第9個</li>            <li style=‘background:blue;‘>第10個</li>        </ul>    </div>    <a href="#" id="prev">向左</a>    <a href="#" id="next">向右</a></div><script>    var prev = document.getElementById(‘prev‘);    var next = document.getElementById(‘next‘);    var parent = document.getElementById(‘parent‘);    var ttt;    var type = true;    var t;    var span;    var ul = document.getElementById(‘ul‘);    var li = ul.getElementsByTagName(‘li‘);    var color1 = ‘#cccccc‘;//小圓點的背景顏色 灰色    var color2 = ‘red‘;//小圓點的背景顏色,紅色    var liWidth = li[0].offsetWidth;    ul.style.width = liWidth*li.length+‘px‘;    //        點點    for(var i = 0;i<li.length;i++){        li[i].index = i;        span = document.createElement(‘span‘);        span.className = ‘span‘;        span.style.background = color1;        span.innerHTML = i+1;        parent.appendChild(span);    }    var span_span = parent.getElementsByTagName(‘span‘);    for(var n = 0;n<span_span.length;n++){        span_span[n].index = n;        //所有的小圓點的事件        span_span[n].onmouseover = function(){            if(type){                ul_ul(this.index);                for(var s = 0;s<span_span.length;s++){                    span_span[s].style.background = color1;                    span_span[this.index].style.background = color2;                }                type = true;            }        }    }    function ul_ul(inde){        var this_li = li[0].index;        if(inde>this_li){            var x = inde-this_li;            for(var y = 0;y<x;y++){                ul.appendChild(li[0]);            }        }        if(inde<this_li){            var x_x = this_li-inde;            for(var g = 0;g<x_x;g++){                ul.insertBefore(li[li.length-1],li[li.length-li.length]);            }        }    }    span_span[0].style.background = color2;    prev.onclick = function(){        if(type){            clearInterval(t);            ul.insertBefore(li[li.length-1],li[li.length-li.length]);            liWidth = li[0].offsetWidth;            ul.style.left = ‘-‘+liWidth+‘px‘;            t = setInterval(rem,5);//動畫速度修改            type = false;            background(0);        }    };    next.onclick = function(){        if(type){            liWidth = 0;            clearInterval(ttt);            ttt = setInterval(nex,5);//動畫速度修改            type = false;            background(1);        }    };    function background(number){        for(var j = 0;j<span_span.length;j++){            span_span[j].style.background = color1;        }        span_span[li[number].index].style.background = color2;    }    function nex(){        ul.style.left = ‘-‘+liWidth+ ‘px‘;        liWidth += 3 ;        if(liWidth == li[0].offsetWidth+1){            clearInterval(ttt);            ul.appendChild(li[li.length-li.length]);            liWidth = 0;            ul.style.left = liWidth+‘px‘;            type = true;        }    }    function rem(){        ul.style.left = ‘-‘+liWidth+‘px‘;        liWidth -= 3 ;        if(liWidth == -1){            clearInterval(t);            type = true;        }    }    var parent_t = setInterval(next.onclick,1500);    parent.onmouseover = function(){        clearInterval(parent_t);    };    parent.onmouseout = function(){        parent_t = setInterval(next.onclick,1500);    };</script></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.