轮播图事件

需求:
1.鼠标不在图片上方时,进行自动轮播,并且左右箭头不会显示;当鼠标放在图片上方时,停止轮播,并且左右箭头会显示;
2.图片切换之后,图片中下方的小圆点会同时进行切换,并且点击相应的小圆点可以切换到相应的图片上;
3.点击左右箭头可以进行左右图片的切换;
4.图片上需有介绍的文字,会随图片切换一起进行切换。
实现原理:
1.获取需要用到的元素
2.设置光标移动箭头的出现与隐藏,声明所有li监听鼠标点击移动事件
3.rightbtn.click()将其添加在定时器中即可完成轮播
代码:

<!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">
    <title>Document</title>
    <style>
        *{
    margin: 0;
    padding: 0;
}
.windows{
    position: relative;
    border: 3px rgb(146, 146, 146) solid;
    width: 700px;
    height: 450px;
    background-color: rgb(250, 186, 186);
    margin: 170px auto;
    overflow: hidden;
}
.leftbtn{
    z-index: 999;
     position: absolute;
     box-sizing: border-box;
     top: 195px;
     left: 0;
     width: 50px;
     height: 70px;
     background-color: rgba(136, 135, 135, 0.582);
     line-height: 70px;
     text-align: center;
     font-size: 35px;
     color: rgba(255, 255, 255, 0.609);
     cursor: pointer;
     display: none;
}
.leftbtn:hover{
    background-color: rgba(189, 186, 186, 0.623);
    color: aliceblue;
}
.rightbtn{
    z-index: 999;
     position: absolute;
     box-sizing: border-box;
     top: 195px;
     right: 0;
     width: 50px;
     height: 70px;
     background-color: rgba(136, 135, 135, 0.609);
     line-height: 70px;
     text-align: center;
     font-size: 35px;
     color: rgba(255, 255, 255, 0.596);
     cursor: pointer;
     display: none;
}
.rightbtn:hover{
    background-color: rgba(189, 186, 186, 0.623);
    color: aliceblue;
}
.imgbox{
   position:absolute;
   left: 0;
   top: 0;
    box-sizing: border-box;
    width: 700%;
    height: 450px;
}
.imgbox li{
    float: left;
    box-sizing: border-box;
    width: 700px;
    height: 450px;
    list-style: none;
}
.imgbox li a{
    box-sizing: border-box;
    display: inline-block;
    width: 700px;
    height: 450px;
    background-color: rgb(134, 152, 255);
}
.imgbox li a img{
    cursor:auto;
    width: 700px;
    height: 450px;
}
.circlebox{
    position: absolute;
    bottom: 0;
    width: 700px;
    height: 40px;
    background-color: rgba(94, 94, 94, 0.486);
 
}
.circle {
    position: absolute;
    bottom: 10px;
    left: 300px;
}
.circle li {
    float: left;
    width: 7px;
    height: 7px;
    list-style: none;
    border: 2px solid rgb(185, 185, 185);
    border-radius:100%;
    margin: 3px;
    cursor: pointer;
    background-color: rgb(173, 173, 173);
}
.circlecolor{
    background-color: coral !important;
    border: 2px solid coral !important;
}
    </style>
</head>
<body>
    <div class="windows">
        <div class="leftbtn"> < </div>
        <div class="rightbtn"> > </div>
        <ul class="imgbox">
            <li>
                <a href="#"><img src="./R-C.png" alt="" class="lunboimg"></a>
            </li>
            <li>
                <a href="#"><img src="./-C.png" alt="" class="lunboimg"></a>
            </li>
            <li>
                <a href="#"><img src="./R-C.png" alt="" class="lunboimg"></a>
            </li>
            <li>
                <a href="#"><img src="./-C.png" alt="" class="lunboimg"></a>
            </li>
            <li>
                <a href="#"><img src="./R-C.png" alt="" class="lunboimg"></a>
            </li>
            <li>
                <a href="#"><img src="./-C.png" alt="" class="lunboimg"></a>
            </li>
        </ul>
        <div class="circlebox">
               <ol class="circle"></ol>
        </div>
    </div>
    <script>
        window.addEventListener('load', function () {
    var leftbtn = document.querySelector('.leftbtn');
    var rightbtn = document.querySelector('.rightbtn');
    var windows = document.querySelector('.windows');
    var circleul = document.querySelector('.imgbox');
    var circleol = document.querySelector('.circle');
    //光标移动至轮播图区域按钮显示,移开隐藏,点击按钮的变化效果
    windows.addEventListener('mouseenter', function () {
        leftbtn.style.display = 'block';
        rightbtn.style.display = 'block';
        clearInterval(timer) //清除定时器自动播放
        timer = null;
    })
    windows.addEventListener('mouseleave', function () {
        leftbtn.style.display = 'none'
        rightbtn.style.display = 'none'
        timer = setInterval(function () {
            rightbtn.click();
        }, 2000)
    })
    leftbtn.addEventListener('click', function () {
        leftbtn.style.color = 'grey'
        var timer = setTimeout(function () {
            leftbtn.style.color = 'aliceblue'
        }, 100)
    })
    rightbtn.addEventListener('click', function () {
        rightbtn.style.color = 'grey'
        var timer = setTimeout(function () {
            rightbtn.style.color = 'aliceblue'
        }, 100)
    })
    //点击小圆圈可以滚动
    for (var i = 0; i < circleul.children.length; i++) {
        lis = document.createElement('li');
        lis.setAttribute('index', i);
        circleol.appendChild(lis);
        lis.addEventListener('click', function () {
            var currentindex = this.getAttribute('index'); //bug整改(3行)
            num = currentindex;
            circlechange = currentindex;
            for (var i = 0; i < circleol.children.length; i++) {
                circleol.children[i].className = '';
            }
            this.className = 'circlecolor';
            var imgwidth = windows.offsetWidth - 6;
            var long = this.getAttribute('index') * imgwidth;
            run(circleul, -long);
        })
    }
    circleol.children[0].className = 'circlecolor';
    //克隆第一张图片至末尾
    var firstimg = circleul.children[0].cloneNode(true);
    circleul.appendChild(firstimg);
 
    //右侧按钮点击滚动
    num = 0;
    circlechange = 0;
    rightbtn.addEventListener('click', function () {
        if (num == circleul.children.length - 1) {
            circleul.style.left = 0;
            num = 0;
        }
        num++;
        long = num * windows.offsetWidth - 6 * num;
        run(circleul, -long);
        //小圆圈跟着一起变化
        circlechange++;
        if (circlechange == circleul.children.length - 1) {
            circlechange = 0;
        }
        for (var i = 0; i < circleol.children.length; i++) {
            circleol.children[i].className = '';
        }
        circleol.children[circlechange].className = 'circlecolor';
    })
    //左侧按钮滚动
    leftbtn.addEventListener('click', function () {
        if (num == 0) {
            circleul.style.left = (circleul.children.length - 1) * windows.offsetWidth;
            num = circleul.children.length - 1;
        }
        num--;
        long = num * windows.offsetWidth - 6 * num;
        run(circleul, -long);
        //小圆圈跟着一起变化
        circlechange--;
        if (circlechange < 0) {
            circlechange = circleol.children.length - 1; //注意此处是ol的子节点的长度-1
        }
        for (var i = 0; i < circleol.children.length; i++) {
            circleol.children[i].className = '';
        }
        circleol.children[circlechange].className = 'circlecolor';
    })
    //自动播放
    var timer = setInterval(function () {
        rightbtn.click();
    }, 2000)
 
})
 
function run(obj, long, callback) {
    clearInterval(obj.timer)
    obj.timer = setInterval(function () {
        if (obj.offsetLeft == long) {
            window.clearInterval(obj.timer);
            if (callback) {
                callback();
            }
        } else {
            step = (long - obj.offsetLeft) / 10
            step = step > 0 ? Math.ceil(step) : Math.floor(step)
            obj.style.left = obj.offsetLeft + step + 'px';
        }
    }, 20)
}
    </script>
</body>
</html>

效果展示:

QQ录屏20230115170029

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值