JS轮播图定时播放,鼠标经过轮播图时不停,鼠标离开轮播图时播放速度越来越快

发布时间 2023-04-06 10:02:32作者: 晓葡萄在路上
<!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>
        *{
            padding: 0px;
            margin: 0px;
        }
        .box2{
            position: relative;
            width: 500px;
            height: 400px;
            border: 10px solid rgb(247, 241, 241);
            box-shadow: 10px 10px 10px #aeaeae;
            overflow: hidden;
        }
        .tup{
            position: absolute;
            width: 3000px;
            height: 100%;
            border: 1px solid blueviolet;
        }
        .tup li{
            list-style: none;
           width: 500px;
           height: 400px;
           /* border: 1px solid burlywood; */
           float: left;

        }
        .tup li:nth-child(1){
            background: url(images/大1.jpg);
          
        }
        .tup li:nth-child(2){
            background: url(images/大2.jpg);
        }
        .tup li:nth-child(3){
            background: url(images/大5.png);
        }
        .tup li:nth-child(4){
            background: url(images/大6.jpg);
        }
        .tup li:nth-child(5){
            background: url(images/大7.jpg);
        }
        .tup li:nth-child(6){
            background: url(images/大8.jpg);
        }
        .right{
            position: absolute;
            top: 50%;
            margin-top: -10px;
            right: 20px;
            width: 20px;
            height: 20px;
            background-color:rgba(0,0,0,0.4);
        }
        a{
            color: aliceblue;
            text-decoration: none;
        }
        .left{
            position: absolute;
            top: 50%;
            margin-top: -10px;
            left: 20px;
            width: 20px;
            height: 20px;
            background-color:rgba(0,0,0,0.4);
        }
      
    </style>
</head>
<body>
    <div class="box2">
        <ul class="tup">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <div class="right">
            <a href="#">></a>
        </div>
        <div class="left">
            <a href="#"><</a>
        </div>
    </div>
    
</body>
<script>
const right=document.querySelector('.right');
const left=document.querySelector('.left');
const ul=document.querySelector('.tup');
const box2=document.querySelector('.box2');
let arr=document.querySelectorAll('.tup li');
console.log(arr)
let index=0;
let width=500;
function stepy(){
    ul.style.left=-index*width+'px';
}
right.addEventListener('click',function(){
    index++;
    //如果这只写大于arr.length,最后会多一个空白li
    if(index>=arr.length){
        index=0;
    }
    stepy();
  
})
left.addEventListener('click',function(){
    index--;
    if(index<0){
        index=5;
    }
    -stepy();
})
let xh=setInterval(function(){
  right.click();
   
    
},1000)
box2.addEventListener('mouseenter',function(){
    clearInterval(xh);
   
    console.log(xh);
})

box2.addEventListener('mouseleave',function(){
    clearInterval(xh);
   setInterval(function(){
    right.click();
},1000)

})



</script>
</html>

  解决方法:鼠标离开时,设置的定时器没有命名,也没有关闭定时器

box2.addEventListener('mouseleave',function(){
    clearInterval(xh);
   xh= setInterval(function(){
    right.click();
},1000)

})