JS实现轮播图

该代码示例展示了如何使用JavaScript和CSS创建一个具有自动播放、鼠标悬停暂停、箭头导航及小圆点指示器的图片轮播图。当鼠标离开图片时,图片会自动切换,鼠标悬停时则停止切换并显示导航箭头。点击小圆点或箭头可手动切换图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求

  1. 鼠标不在图片上方时,进行自动轮播,并且左右箭头不会显示;
  2. 当鼠标放在图片上方时,停止轮播,并且左右箭头会显示;
  3. .图片切换之后,图片中下方的小圆点会同时进行切换,并且点击相应的小圆点可以切换到相应的图片上;
  4. 点击左右箭头可以进行左右图片的切换;
  5. 图片上需有介绍的文字,会随图片切换一起进行切换。

效果展示

JS实现轮播图效果

实现原理

  • 与TAB选项卡同理给图片绑定active样式使其能够显示在最上层,利用goIndex给图片和对应的小圆点同时加上active
  • 设置监听鼠标移动事件,在鼠标移入时清除定时器
  • 用getAttribute拿到刚才给li标签绑定的data-index属性,绑定图片index = pointindex,就能实现点击小圆点图片切换了

具体代码

JS部分

    <script>
        var items = document.querySelectorAll(".item");
        var points = document.querySelectorAll(".point")
        var left = document.getElementById("leftBtn");
        var right = document.getElementById("rightBtn");
        var all = document.querySelector(".box")
        var index = 0;
        var time = 0;
        
 
        //清除active
        var clearActive = function () {
            for (i = 0; i < items.length; i++) {
                items[i].className = 'item';
            }
            for (j = 0; j < points.length; j++) {
                points[j].className = 'point';
            }
        }
 
        //改变active
        var goIndex = function () {
            clearActive();
            items[index].className = 'item active';
            points[index].className = 'point active'
        }
        //左按钮事件
        var goLeft = function () {
            if (index == 0) {
                index = 4;
            } else {
                index--;
            }
            goIndex();
        }
 
        //右按钮事件
        var goRight = function () {
            if (index < 4) {
                index++;
            } else {
                index = 0;
            }
            goIndex();
        }
        
 
        //绑定点击事件监听
        left.addEventListener('click', function () {
            goLeft();
            time = 0;//计时器跳转清零
        })
 
        right.addEventListener('click', function () {
            goRight();
            time = 0;//计时器跳转清零
        })
 
        for(i = 0;i < points.length;i++){
            points[i].addEventListener('click',function(){
                var pointIndex = this.getAttribute('data-index')
                index = pointIndex;
                goIndex();
                time = 0;//计时器跳转清零
            })
        }
        
        var timer;
        function play(){
         timer = setInterval(() => {
            time ++;
            if(time == 20 ){
                goRight();
                time = 0;
            }    
        },100)
    }
        play();
        //移入清除计时器
        all.onmousemove = function(){
            clearInterval(timer)
        }
       //移出启动计时器
        all.onmouseleave = function(){
            play();
        }
    </script>

完整代码

<!DOCTYPE html>
<html>
 
<head>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .box {
            margin: 30px 100px;
            width: 1350px;
            height: 800px;
            position: relative;
        }
 
        .list {
            width: 1350px;
            height: 800px;
            position: relative;
            padding-left: 0px;
        }
        .item {
            width: 100%;
            height: 450px;
            padding-top: 350px;
            position: absolute;
            left: 0;
            color: rgb(36, 35, 79);
            font-size: 50px;
            text-align: center;
            transition: all .8s;
        }
 
        .item:nth-child(1) {
            background-color: palevioletred;
        }
 
        .item:nth-child(2) {
            background-color: yellowgreen;
        }
 
        .item:nth-child(3) {
            background-color: khaki;
        }
 
        .item:nth-child(4) {
            background-color: pink;
        }
 
        .item:nth-child(5) {
            background-color: orange;
        }
 
        .item.active {
            z-index: 10;
        }
 
        .btn {
            width: 50px;
            height: 200px;
            top: 280px;
            position: absolute;
            opacity: 0.5;
            z-index: 100;
        }
 
        #leftBtn {
            left: 0px;
        }
 
        #rightBtn {
            right: 0px;
        }
 
        .pointList {
            list-style: none;
            padding-right: 340px;
            position: absolute;
            right: 20px;
            bottom: 20px;
            z-index: 200;
        }
 
        .point {
            width: 10px;
            height: 10px;
            background-color: antiquewhite;
            border-radius: 100%;
            float: left;
            margin-right: 80px;
            border-style: solid;
            border-width: 2px;
            border-color: #b9b9b9;
            cursor: pointer;  
        }
 
        .point.active{
            background-color: rgb(92, 92, 92);
        }
    </style>
</head>
 
<body>
    <div class="box">
        <ul class="list">
            <li class="item active">first&nbsp;slide</li>
            <li class="item">second&nbsp; slide</li>
            <li class="item">third&nbsp; slide</li>
            <li class="item">fourth&nbsp;slide</li>
            <li class="item">fifth&nbsp; slide</li>
        </ul>
        <ul class="pointList">
            <li class="point active" data-index = 0></li>
            <li class="point" data-index = 1></li>
            <li class="point" data-index = 2></li>
            <li class="point" data-index = 3></li>
            <li class="point" data-index = 4></li>
        </ul>
        <button class="btn" id="leftBtn"> < </button> 
        <button class="btn" id="rightBtn"> > </button>
 
    </div>
    <script>
        var items = document.querySelectorAll(".item");
        var points = document.querySelectorAll(".point")
        var left = document.getElementById("leftBtn");
        var right = document.getElementById("rightBtn");
        var all = document.querySelector(".box")
        var index = 0;
        var time = 0;
        
 
        //清除active
        var clearActive = function () {
            for (i = 0; i < items.length; i++) {
                items[i].className = 'item';
            }
            for (j = 0; j < points.length; j++) {
                points[j].className = 'point';
            }
        }
 
        //改变active
        var goIndex = function () {
            clearActive();
            items[index].className = 'item active';
            points[index].className = 'point active'
        }
        //左按钮事件
        var goLeft = function () {
            if (index == 0) {
                index = 4;
            } else {
                index--;
            }
            goIndex();
        }
 
        //右按钮事件
        var goRight = function () {
            if (index < 4) {
                index++;
            } else {
                index = 0;
            }
            goIndex();
        }
        
 
        //绑定点击事件监听
        left.addEventListener('click', function () {
            goLeft();
            time = 0;//计时器跳转清零
        })
 
        right.addEventListener('click', function () {
            goRight();
            time = 0;//计时器跳转清零
        })
 
        for(i = 0;i < points.length;i++){
            points[i].addEventListener('click',function(){
                var pointIndex = this.getAttribute('data-index')
                index = pointIndex;
                goIndex();
                time = 0;//计时器跳转清零
            })
        }
        
        var timer;
        function play(){
         timer = setInterval(() => {
            time ++;
            if(time == 20 ){
                goRight();
                time = 0;
            }    
        },100)
    }
        play();
        //移入清除计时器
        all.onmousemove = function(){
            clearInterval(timer)
        }
       //移出启动计时器
        all.onmouseleave = function(){
            play();
        }
    </script>
</body>
 
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值