• 原生JS制作最简单轮播图(超清晰思路)


    使用html+css将页面制作完毕后,完成页面自动轮播,左右箭头点击图片改变,下方小按钮悬浮更改图片。

    banner

    制作思路:

    CSS:

    1、鼠标进入banner后左右箭头出现。

    .banner:hover #leftbutton {
              visibility: visible;
    }
    
    • 1
    • 2
    • 3

    2、页面加载初始定义一个小按钮为红色初始状态。

     
    "button">
    • "buttonindex1" style="background-color: red">
    • "buttonindex2">
    • "buttonindex3">
    • "buttonindex4">
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Javascript:

    一、自动轮播
    1、核心是计时器

    setInterval(banner, 3000); 
    
    • 1

    2、每次执行函数banner(),改变图片src,小按钮backgroundColor,这里就需要定义一个计数器来更改图片路径与小按钮伪数组地址,也是将图片地址与按钮绑定起来。

    var index = 1  //加载banner()函数外面,不然每次执行函数index都会重置
    
    • 1
    setInterval(function banner() {
            index++;  //每次执行index加1,当大于4是,重置为1
            if (index > 4) {
                index = 1;
            }
            //获取元素,更换src
            var img = document.querySelector(".img").querySelector("img");
            var button = document.querySelectorAll("li");
            img.src = "./img/back" + index + ".jpg";
            for (i = 0; i < button.length; i++) {     //遍历数组全部重置成白色
                button[i].style.backgroundColor = "white"
            }
            button[index-1].style.backgroundColor = "red";
        }, 3000); //3s自动轮播
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    二、左右箭头
    1、点击箭头更换图片,改变按钮颜色。

    左箭头index自减

    leftbutton.onclick = function() {
                index--;
                if (index < 1) {
                    index = 4;
                }
                img.src = "./img/back" + index + ".jpg";
                for (i = 0; i < button.length; i++) {
                    button[i].style.backgroundColor = "white"
                }
                button[index-1].style.backgroundColor = "red";
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    右箭头index自增

    rightbutton.onclick = function() {
            index++;
            if (index > 4) {
                index = 1;
            }
            img.src = "./img/back" + index + ".jpg";
            for (i = 0; i < button.length; i++) {
                button[i].style.backgroundColor = "white"
            }
            button[index-1].style.backgroundColor = "red";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    三、小按钮
    1、鼠标悬浮,更换成对应图片路径,按钮颜色。这里应该可以按照数组遍历来写,但是没写。

      //小按钮1   这样事件有四个,复制后修改对应对象就可以
        buttonindex1.onmouseover = function() {
            for (i = 0; i < button.length; i++) {
                button[i].style.backgroundColor = "white"
            }
            buttonindex1.style.backgroundColor = "red"
            index = 1;
            img.src = "./img/back" + index + ".jpg";
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    附录:

    HTML:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="./index.css">
        <script src="./index.js"></script>
        <link rel="stylesheet" href="../表单/img/font-awesome-4.7.0/css/font-awesome.min.css">
    
        <title>主页</title>
    </head>
    
    <body>
        <div class="clock">
            <h1></h1>
        </div>
        <div class="banner">
            <div class="bannerimg">
                <ul>
                    <li class="img">
                        <a href=""><img src="./img/back1.jpg" alt=""></a>
                    </li>
                </ul>
            </div>
            <div class="button">
                <ul>
                    <li class="buttonindex1" style="background-color: red"></li>
                    <li class="buttonindex2"></li>
                    <li class="buttonindex3"></li>
                    <li class="buttonindex4"></li>
                </ul>
            </div>
            <div id="leftbutton">
                <i class="fa fa-chevron-left" aria-hidden="true"></i>
            </div>
            <div id="rightbutton">
                <i class="fa fa-chevron-right" aria-hidden="true"></i>
            </div>
        </div>
    </body>
    
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    CSS:

    * {
        margin: 0;
        padding: 0;
    }
    
    ul li {
        list-style: none;
        float: left;
        cursor: pointer;
        margin: 0 12px;
    }
    
    a {
        text-decoration: none;
    }
    
    body {
        background-color: #f4f4f4;
    }
    
    .bannerimg .img img {
        width: 1000px;
        height: 650px;
        z-index: -100;
        margin-left: -12px;
    }
    
    .clock {
        text-align: center;
    }
    
    .banner {
        width: 1000px;
        height: 650px;
        margin: 0 auto;
        /* border: 1px solid red; */
        position: relative;
        /* top: 100px;
        left: 450px; */
        overflow: hidden;
    }
    
    .button {
        position: absolute;
        right: 420px;
        bottom: 40px;
        z-index: 100;
        width: 156px;
        height: 25px;
        border-radius: 20px;
        background-color: rgba(255, 255, 255, .3);
    }
    
    .button ul {
        margin-top: 5px;
    }
    
    .button li {
        width: 14px;
        height: 14px;
        border-radius: 50%;
        /* border: 1px solid white; */
        background-color: white;
        line-height: 40px;
    }
    
    #leftbutton {
        position: absolute;
        left: -16px;
        top: 290px;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, .15);
        color: rgba(255, 255, 255, .8);
        font-size: 26px;
        cursor: pointer;
        visibility: hidden;
    }
    
    #leftbutton i {
        margin: 14px 18px;
    }
    
    .banner:hover #leftbutton {
        visibility: visible;
    }
    
    #leftbutton:hover {
        background-color: rgba(0, 0, 0, .20);
    }
    
    #rightbutton {
        position: absolute;
        right: -16px;
        top: 290px;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, .15);
        color: rgba(255, 255, 255, .8);
        font-size: 26px;
        cursor: pointer;
        visibility: hidden;
    }
    
    #rightbutton i {
        margin: 14px 14px;
    }
    
    .banner:hover #rightbutton {
        visibility: visible;
    }
    
    #rightbutton:hover {
        background-color: rgba(0, 0, 0, .20);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    JS:

    //页面加载
    window.onload = function() {
        //定义index计数增加
    
        var index = 1
        var leftbutton = document.querySelector("#leftbutton");
        var rightbutton = document.querySelector("#rightbutton");
        var img = document.querySelector(".img").querySelector("img");
        var button = document.querySelector(".button").querySelectorAll("li");
        var buttonindex1 = document.querySelector(".buttonindex1");
        var buttonindex2 = document.querySelector(".buttonindex2");
        var buttonindex3 = document.querySelector(".buttonindex3");
        var buttonindex4 = document.querySelector(".buttonindex4");
    
        setInterval(function banner() {
            index++;
            if (index > 4) {
                index = 1;
            }
            //获取元素,更换src
    
            img.src = "./img/back" + index + ".jpg";
    
            for (i = 0; i < button.length; i++) {
                button[i].style.backgroundColor = "white"
            }
            button[index - 1].style.backgroundColor = "red";
    
    
        }, 3000); //3s自动轮播
    
        //左箭头
        leftbutton.onclick = function() {
                index--;
                if (index < 1) {
                    index = 4;
                }
                img.src = "./img/back" + index + ".jpg";
                for (i = 0; i < button.length; i++) {
                    button[i].style.backgroundColor = "white"
                }
                button[index - 1].style.backgroundColor = "red";
            }
            //右箭头
        rightbutton.onclick = function() {
            index++;
            if (index > 4) {
                index = 1;
            }
            img.src = "./img/back" + index + ".jpg";
            for (i = 0; i < button.length; i++) {
                button[i].style.backgroundColor = "white"
            }
            button[index - 1].style.backgroundColor = "red";
        }
        //小按钮1
        buttonindex1.onmouseover = function() {
            for (i = 0; i < button.length; i++) {
                button[i].style.backgroundColor = "white"
            }
            buttonindex1.style.backgroundColor = "red"
            index = 1;
            img.src = "./img/back" + index + ".jpg";
    
        }
    
        //小按钮2
        buttonindex2.onmouseover = function() {
            for (i = 0; i < button.length; i++) {
                button[i].style.backgroundColor = "white"
            }
            buttonindex2.style.backgroundColor = "red"
            index = 2;
            img.src = "./img/back" + index + ".jpg";
        }
    
        //小按钮3
        buttonindex3.onmouseover = function() {
            for (i = 0; i < button.length; i++) {
                button[i].style.backgroundColor = "white"
            }
            buttonindex3.style.backgroundColor = "red"
            index = 3;
            img.src = "./img/back" + index + ".jpg";
        }
    
        //小按钮4
        buttonindex4.onmouseover = function() {
            for (i = 0; i < button.length; i++) {
                button[i].style.backgroundColor = "white"
            }
            buttonindex4.style.backgroundColor = "red"
            index = 4;
            img.src = "./img/back" + index + ".jpg";
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
  • 相关阅读:
    中秋时节赏明月,五子棋戏月饼趣 — Flutter中秋限定版五子棋
    wordpress添加评论过滤器
    C++读写Excel(xlnt库的使用)
    python个人bug修炼手册
    Elementui的el-dropdown组件使用与案例
    不重复数字
    【912.排序数组】
    Unity-CharacterController(角色控制器)
    【云原生】Kubeadmin安装k8s集群
    【初始MongoDB】MongoDB的使用(对比MySQL)
  • 原文地址:https://blog.csdn.net/weixin_43205774/article/details/126198289