用JavaScript實(shí)現(xiàn)輪播圖效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)代碼:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
        #box {
            margin: 30px auto;
            width: 590px;
            height: 340px;
            position: relative;
        }
 
        #banner-list > li {
            position: absolute;
            left: 0;
            right: 0;
            opacity: 0;
            /*過(guò)渡動(dòng)畫(huà)*/
            transition: opacity 2s ease;
        }
 
        #left, #right {
            width: 30px;
            height: 60px;
            text-align: center;
            line-height: 60px;
            font-size: 24px;
            color: rgba(255,255,255,0.7);
            background-color: rgba(0,0,0,0.3);
            position: absolute;
            top: 50%;
            margin-top: -30px;
            z-index: 3;
        }
 
        #right {
            right: 0;
        }
 
        #tag-list {
            width: 130px;
            position: absolute;
            left: 50%;
            bottom: 8px;
            margin-left: -55px;
        }
 
        #tag-list > li {
            float: left;
            width: 18px;
            height: 18px;
            margin: 4px;
            text-align: center;
            line-height: 18px;
            font-size: 13px;
            background-color: white;
            border-radius: 9px;
            /*過(guò)渡動(dòng)畫(huà)*/
            transition: background-color 1s ease;
        }
    </style>
    <script>
        window.onload = function (){
            //獲取tag_list和圓圈list
            var tag_list = document.getElementById("tag-list");
            var list = tag_list.children;
 
            //1.獲取 ul(banner_list) 和 所有的li
            let banner_list = document.getElementById("banner-list");
            let bannerLi = banner_list.children;
 
            //2.默認(rèn)顯示第一張banner
            bannerLi[0].className = "current-banner"
            bannerLi[0].style.opacity = 1
            list[0].style.backgroundColor = "red"
 
            //3.索引從0開(kāi)始,默認(rèn)顯示第一張。
            //count表示當(dāng)前顯示頁(yè)面的索引
            let count = 0;
 
            //4.點(diǎn)擊>向右切換
            var right = document.getElementById("right");
            right.onclick = function (){
                //4.1先將當(dāng)前頁(yè)面隱藏
                bannerLi[count].className = ""
                bannerLi[count].style.opacity = 0
                list[count].style.backgroundColor = "white"
 
                //4.2.頁(yè)碼加1,當(dāng)?shù)降?張(index=5),切換到第一張(index=0)
                if (++count == 5){
                    count = 0
                }
 
                //4.3 設(shè)置當(dāng)前頁(yè)碼為顯示
                bannerLi[count].className = "current-banner"
                bannerLi[count].style.opacity = 1
                list[count].style.backgroundColor = "red"
            }
 
            //和right差不多,修改下條件
            var left = document.getElementById("left");
            left.onclick = function (){
                //4.1先將當(dāng)前頁(yè)面隱藏
                bannerLi[count].className = ""
                bannerLi[count].style.opacity = 0
                list[count].style.backgroundColor = "white"
 
                //4.2.頁(yè)碼減1,當(dāng)?shù)降?張(index=-1),切換到第5張(index=4)
                if (--count == -1){
                    count = 4
                }
 
                //4.3 設(shè)置當(dāng)前頁(yè)碼為顯示
                bannerLi[count].className = "current-banner"
                bannerLi[count].style.opacity = 1
                list[count].style.backgroundColor = "red"
            }
 
            //給所有圓圈綁定鼠標(biāo)事件
            for (let i = 0; i < list.length; i++) {
                list[i].onmouseenter= function (){
                    //設(shè)置圓圈樣式
                    list[count].style.backgroundColor = "white"
                    list[i].style.backgroundColor = "red"
                    //設(shè)置banner圖樣式
                    bannerLi[count].className = ""
                    bannerLi[count].style.opacity = 0
                    bannerLi[i].className = "current-banner"
                    bannerLi[i].style.opacity = 1
                    //將count置為i
                    count = i
                }
            }
        }
    </script>
</head>
<body>
    <div id="box">
        <ul id="banner-list">
            <li class="current-banner"><img src="banner-img/11.jpg" alt=""></li>
            <li><img src="banner-img/22.jpg" alt=""></li>
            <li><img src="banner-img/33.jpg" alt=""></li>
            <li><img src="banner-img/44.jpg" alt=""></li>
            <li><img src="banner-img/55.jpg" alt=""></li>
        </ul>
        <span id="left"><</span>
        <span id="right">></span>
        <div>
            <ul id="tag-list">
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
            </ul>
        </div>
    </div>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript實(shí)現(xiàn)簡(jiǎn)易輪播圖最全代碼解析(ES5)
 - 用js實(shí)現(xiàn)輪播圖效果
 - 原生js實(shí)現(xiàn)簡(jiǎn)單輪播圖效果
 - 使用JavaScript實(shí)現(xiàn)輪播圖特效
 - js實(shí)現(xiàn)輪播圖的完整代碼
 - 原生js實(shí)現(xiàn)輪播圖的示例代碼
 - js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果實(shí)例
 - JS輪播圖實(shí)現(xiàn)簡(jiǎn)單代碼
 - js實(shí)現(xiàn)點(diǎn)擊左右按鈕輪播圖片效果實(shí)例
 - JavaScript實(shí)現(xiàn)簡(jiǎn)易輪播圖最全代碼解析(ES6面向?qū)ο?
 
相關(guān)文章
 Javascript驗(yàn)證上傳圖片大小[前臺(tái)處理]
在做上傳圖片的時(shí)候,如果不限制上傳圖片大小,后果非常的嚴(yán)重。解決這個(gè)問(wèn)題有兩種方式:后臺(tái)處理、前臺(tái)處理2014-07-07
 Uncaught?SyntaxError:Unexpected?token?'<'?(
這篇文章主要為大家介紹了JS判斷趨近于直線的多邊形(退化多邊形)實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
 javascript解析ajax返回的xml和json格式數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了javascript解析ajax返回的xml和json格式數(shù)據(jù),結(jié)合實(shí)例形式詳細(xì)分析了JS ajax調(diào)用及返回值中xml與json格式數(shù)據(jù)的處理技巧,需要的朋友可以參考下2017-01-01
 javascript中字體浮動(dòng)效果的簡(jiǎn)單實(shí)例演示
這篇文章主要介紹了javascript中字體浮動(dòng)效果的簡(jiǎn)單實(shí)例演示,在一些網(wǎng)站上經(jīng)常遇到當(dāng)鼠標(biāo)移導(dǎo)航欄的時(shí)候,能夠使其彈出下拉選項(xiàng),現(xiàn)在就演示一下這種功能,感興趣的小伙伴們可以參考一下2015-11-11
 200行代碼實(shí)現(xiàn)blockchain 區(qū)塊鏈實(shí)例詳解
這篇文章主要介紹了200行代碼實(shí)現(xiàn)blockchain 區(qū)塊鏈的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-03-03
 showModalDialog 和 showModelessDialog
showModalDialog 和 showModelessDialog...2007-01-01
 js select option對(duì)象小結(jié)
本篇文章主要是對(duì)js中的select option對(duì)象進(jìn)行了總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12

