原生JS 实现轮播图效果

轮播图的实现

轮播图是我们在浏览网站的时候很常见的一个部分,比如小米商城,京东商城等等,轮播图可以使网页见面变得非常漂亮,所以学习JS的时候,轮播图是必不缺少的一部分。

我在此分三个部分,html,css,js。

HTML部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="style.css">
    <title>轮播图的实现</title>
</head>
<body>
    <div class="banner">
        <ul class="images">
            <li class="active">
                <img src="imgs/57523a17c02ad.jpg" alt="">
            </li>
            <li>
                <img src="imgs/59c898949b98b.jpg" alt="">
            </li>
            <li>
                <img src="imgs/5bbd6379b2b20.jpg" alt="">
            </li>
            <li>
                <img src="imgs/81a9a1506c024141ea5ea8888e935ca3bbd7bf56.jpg" alt="">
            </li>
            <li>
                <img src="imgs/one.jpg" alt="">
            </li>
            <li>
                <img src="imgs/vkleVabkBxvBupx.jpg" alt="">
            </li>
        </ul>
        <ul class="dots">
            <li class="white"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <i id="left" class="fa fa-chevron-left fa-3x"></i>
        <i id="right" class="fa fa-chevron-right fa-3x"></i>
    </div>
</body>
<script src="style.js"></script>
</html>

CSS部分:

* {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
}

body {
    background-color: gray;
}

div {
    width: 700px;
    height: 400px;
    margin: 100px auto;
    position: relative;
}

ul {
    list-style: none;
}

.images li {
    position: absolute;
    opacity: 0;
    transition: 0.6s;
}

.images .active {
    opacity: 1;
}

.images img {
    width: 700px;
    height: 400px;
    cursor: pointer;
}

.dots {
    position: absolute;
    bottom: 20px;
    left: 250px;
}

.dots li {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    border: solid 2px #fff;
    display: inline-block;
    margin-right: 10px;
    cursor: pointer;
    transition: 0.6s;
}

.dots .white {
    background-color: #fff;
    box-shadow: 0 0 1px 5px rgba(0, 0, 0, 0.3)
}
.fa{
    color: #fff;
    cursor: pointer;
    /* background-color:rgb(29, 26, 26); */
    width: 40px;
    height: 60px;
    text-align: center;
    line-height: 60px;
    opacity: 0.3;
}
.fa:hover{
    opacity: 0.6;
}
#left{
    position: absolute;
    top: 180px;
}
#right{
    position: absolute;
    top: 180px;
    right: 0;
}

JS部分:

var lisImg = document.querySelectorAll(".images li");
var lisDot = document.querySelectorAll(".dots li");
var banner = document.querySelector("div");

var left = document.getElementById("left");
var right = document.getElementById("right");

var timer;
var number = 0;

function show() {
    for (var j = 0; j < lisDot.length; j++) {
        lisDot[j].className = "";
        lisImg[j].className = "";
    }
    lisDot[number].className = "white";
    lisImg[number].className = "active";
}
show()

// 自动循环播放
// 往下轮播下一张
var next = function () {
    number++;
    if (number >= lisDot.length) {
        number = 0;
    }
    show();
}
timer = setInterval(next, 1500);

// 往上轮播上一张
// function prev(){
//     timer = setInterval(function(){
//         number--;
//         if(number < 0){
//             number = lisDot.length - 1;
//         }
//         show();
//     }, 1000);
// }
// prev()

// 当鼠标放在图片上时,停止自动轮播,清除定时器
banner.onmousemove = function () {
    clearInterval(timer);
}
// 当鼠标离开图片时,继续进行自动轮播
banner.onmouseleave = function () {
    timer = setInterval(next, 1500);
}


// 滑点轮播
for (var i = 0; i < lisDot.length; i++) {
    lisDot[i].index = i;
    lisDot[i].onmousemove = function () {
        // clearInterval(timer);
        for (var k = 0; k < lisDot.length; k++) {
            lisDot[k].className = "";
            lisImg[k].className = "";
        }
        this.className = "white";
        lisImg[this.index].className = "active";
    }
    // 鼠标离开当前滑点时,依然按照顺序轮播
    lisDot[i].onmouseleave = function () {
        number = this.index;
        timer = setInterval(next, 1500);
    }
}

// 左右键轮播效果
// 上一张
left.onclick = function () {
    number--;
    if (number < 0) {
        number = lisDot.length - 1;
    }
    show();
}

// 下一张
right.onclick = function () {
    number++;
    if (number >= lisDot.length) {
        number = 0;
    }
    show();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jay丶萧邦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值