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

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

banner

制作思路:

CSS:

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

.banner:hover #leftbutton {
          visibility: visible;
}

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

 <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>

Javascript:

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

setInterval(banner, 3000); 

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

var index = 1  //加载banner()函数外面,不然每次执行函数index都会重置
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、点击箭头更换图片,改变按钮颜色。

左箭头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";
        }

右箭头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、鼠标悬浮,更换成对应图片路径,按钮颜色。这里应该可以按照数组遍历来写,但是没写。

  //小按钮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";

    }

附录:

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>

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);
}

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
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柒月VII

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值