换图式轮播图

108 阅读1分钟
<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;
        }
        span{
            position:absolute;
            left:0px;
            top:170px;
            width: 50px;
            height: 50px;
            background-color:#7f7f7f;
            font-size: 30px;
            line-height: 50px;
            text-align: center;
            opacity:0.5;
        }
        #right{
            left:770px;
        }
        #picNav{
            position:absolute;
            top:300px;
            left:300px;
        }
        #picNav li{
            width: 20px;
            height: 20px;
            background-color: #ccc;
            float: left;
            margin-right:20px;
            list-style: none;
            text-align: center;
            line-height: 20px;
            opacity:0.8;
        }
        #picNav .active{
            background-color: yellow;
        }
    </style>
</head>
<body>
    <img src="images/1.png" alt="" id="pic">
    <span id="left">&lt;</span>
    <span id="right">&gt;</span>
    <ul id="picNav">
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul>
    <script>
        var picNav=document.getElementById("picNav");
        var picNavLis=picNav.getElementsByTagName("li");
        var left=document.getElementById("left");
        var rigth=document.getElementById("right");
        var arr=["images/1.png","images/2.jpg","images/3.jpeg","images/4.jpeg","images/5.jpeg"];
        var index=0;
        var pic=document.getElementById("pic");
        function change(){
            if(index==4){
                index=0;
            }else{
                index++;
            }
           pic.src=arr[index];
          for(var n=0;n<picNavLis.length;n++){
              picNavLis[n].className="";
          }
          picNavLis[index].className="active";
        }
       var t=setInterval(change,1000);
       left.onclick=function(){
           if(index==0){
               index=4;
           }else{
               index--;
           }
           pic.src=arr[index];
           for(var n=0;n<picNavLis.length;n++){
              picNavLis[n].className="";
          }
          picNavLis[index].className="active";
       }
       right.onclick=function(){
           if(index==4){
               index=0;
           }else{
               index++;
           }
           pic.src=arr[index];
           for(var n=0;n<picNavLis.length;n++){
              picNavLis[n].className="";
          }
          picNavLis[index].className="active";
       }
       for(var n=0;n<picNavLis.length;n++){
            picNavLis[n].index=n;
            picNavLis[n].onmouseenter=function(){
                for(var j=0;j<picNavLis.length;j++){
                picNavLis[j].className="";
                }
                this.className="active";
                index=this.index;
                pic.src=arr[index];
           }
       }
       pic.onmouseenter=function(){
           clearInterval(t);
       }
       pic.onmouseleave=function(){
           t=setInterval(change,1000);
       }
       left.onmouseenter=function(){
           clearInterval(t);
       }
       left.onmouseleave=function(){
           t=setInterval(change,2000);
       }
       right.onmouseenter=function(){
        clearInterval(t);
       }
       right.onmouseleave=function(){
           t=setInterval(change,2000);
       }
       picNav.onmouseenter=function(){
          clearInterval(t);
       }
       picNav.onmouseleave=function(){
        t=setInterval(change,2000);
       }
    </script>
</body>
</html>