輪播圖簡單實現(轉載)

原文鏈接:https://juejin.im/entry/58ec67d88d6d81006c983ab5

文章轉載自: 十五分鐘用 JavaScript 基礎寫一個圖片輪播效果 + 思路詳解 (原文有詳細講解)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>slideshow</title>
    <style>
        img {
            width: 100%;
        }

        .li_img {
            width: 800px;
            float: left;
            list-style: none;
        }

        .ul_img {
            width: 6000px;
            padding: 0px;
            margin: 0px;
            transition: all 2s;
        }

        .main_div {
            width: 800px;
            overflow: hidden;
            position: relative;
            top: 100px;
            left: 350px;
        }

        .arrows {
            z-index: 9999;
            position: absolute;
            padding-top: 230px;
            width: 800px;
        }

        .arrows span {
            font-size: 3em;
            color: seashell;
        }

        .arrows span:hover {
            /*變小手*/
            cursor: pointer;
            background-color: rgba(192, 192, 192, 0.29);
        }

        .div_btn {
            float: left;
            border-radius: 100px;
            background-color: aquamarine;
            width: 60px;
            height: 10px;
            margin-left: 10px;
            margin-top: 130px;
        }

        .div_btn:hover {
            background-color: aqua;

        }
    </style>
    <script>
        var count = 0;
        var isgo = false;
        var timer;

        window.onload = function () {
            var ul_img = document.getElementsByClassName("ul_img")[0];
            var li_img = document.getElementsByClassName("li_img");
            var arrow = document.getElementsByClassName("arrow");
            var div_btn = document.getElementsByClassName("div_btn");
            div_btn[0].style.backgroundColor = "aqua";


            showtime();

            function showtime() {
                timer = setInterval(function () {
                    if (isgo == false) {
                        count++;
                        ul_img.style.transform = "translate(" + -800 * count + "px)";
                        if (count >= li_img.length - 1) {
                            count = li_img.length - 1;
                            isgo = true;
                        }
                    } else {
                        count--;
                        ul_img.style.transform = "translate(" + -800 * count + "px)";
                        if (count <= 0) {
                            count = 0;
                            isgo = false;
                        }
                    }
                    for (var i = 0; i < div_btn.length; i++) {
                        div_btn[i].style.backgroundColor = "aquamarine";
                    }
                    div_btn[count].style.backgroundColor = "aqua";

                }, 4000)
            }


            for (var i = 0; i < arrow.length; i++) {
                arrow[i].onmouseover = function () {
                    clearInterval(timer);
                }
                arrow[i].onmouseout = function () {
                    showtime();
                }
                arrow[i].onclick = function () {
                    if (this.title == 0) {
                        count++;
                        if (count > 3) {
                            count = 0;
                        }
                    } else {
                        count--;
                        if (count < 0) {
                            count = 3;
                        }
                    }

                    ul_img.style.transform = "translate(" + -800 * count + "px)";

                    for (var i = 0; i < div_btn.length; i++) {
                        div_btn[i].style.backgroundColor = "aquamarine";
                    }
                    div_btn[count].style.backgroundColor = "aqua";
                }
            }
            for (var b = 0; b < div_btn.length; b++) {
                div_btn[b].index = b;
                div_btn[b].onmouseover = function () {

                    clearInterval(timer);

                    for (var a = 0; a < div_btn.length; a++) {
                        div_btn[a].style.backgroundColor = "aquamarine";
                    }
                    div_btn[this.index].style.backgroundColor = "aqua";
                    if (this.index == 3) {
                        isgo = true;
                    }
                    if (this.index == 0) {
                        isgo = false;
                    }
                    count = this.index;
                    ul_img.style.transform = "translate(" + -800 * this.index + "px)";
                }
                div_btn[b].onmouseout = function () {
                    showtime();
                }
            }
        }
    </script>
</head>
<body>
<div class="main_div">
    <div class="arrows">
        <span title="1" class="arrow"><</span>
        <span title="0" class="arrow" style="float: right">></span>
    </div>

    <ul class="ul_img">
        <li class="li_img"><img src="images/1.jpg"></li>
        <li class="li_img"><img src="images/2.jpg"></li>
        <li class="li_img"><img src="images/3.jpg"></li>
        <li class="li_img"><img src="images/4.jpg"></li>
    </ul>
</div>

<div style="margin-left: 600px">
    <div class="div_btn"></div>
    <div class="div_btn"></div>
    <div class="div_btn"></div>
    <div class="div_btn"></div>
</div>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章