js轮播图

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    <style>
    *{
        padding: 0;
        margin: 0;
    }
    ol,ul{
        list-style: none;
    }
    .text{
        width: 400px;
        height: 400px;
        margin: 50px auto;
        overflow: hidden;
        position: relative;
    }
    ul{
        width: 400px;
        height: 400px;
    
        overflow: hidden;
        position: relative;
    }
    ul li{
        width: 400px;
        height: 400px;
        position: absolute;
        opacity: 0;
    }
    .show{
        opacity: 1;
        z-index: 1;
    }
    img{
        width: 100%;
        height: 100%;

    }
    .btn1{
        position: absolute;
        top:45%;
        left: 0;
        z-index: 2;
        font-size: 30px;
        background-color: aqua;
        opacity: 0.5;
        cursor: pointer;
            }
            .btn2{
                position: absolute;
                top:45%;
                right: 0;
                z-index: 2;
                font-size: 30px;
                background-color: aqua;
                opacity: 0.5;
                cursor: pointer;
                    }
        ol{
            width: 200px;
            height: 50px;
            top: 90%;
            left: 30%;
            position: absolute;
            z-index: 2;
            
        }
        ol li{
            width: 20px;
            height: 20px;
            background-color: hotpink;
            float: left;
            border-radius: 50%;
            text-align: center;
            line-height: 20px;
            margin-right: 10px;
            opacity: 0.8;
            cursor: pointer;
        }
        .active{
            background-color: aquamarine;
        }
    </style>
    </head>
    <body> 
    <div class="text">
     <ul>
         <li  class="show"><img src="img/img1.jpg"/></li>
         <li><img src="img/img2.jpg" /></li>
         <li><img src="img/img3.jpg"/></li>
         <li><img src="img/img4.jpg"/></li>
         <li><img src="img/img5.jpg"/></li>
     </ul>
      <span class="btn1">&lt;</span>
      <span class="btn2">&gt;</span>
      <ol>
           <li class="active">1</li>
           <li>2</li>
           <li>3</li>
           <li>4</li>
           <li>5</li>
      </ol>
      </div>
    <script>
    var img=document.querySelectorAll('ul li')
    var li=document.querySelectorAll('ol li')
    var span=document.querySelectorAll('span')
    var ul=document.querySelector('ul')
    var index=0
    span[0].οnclick=function(){
        if(index==-1){
            index=img.length-1
        }
        for(let i=0;i<img.length;i++){
            img[i].className=''
            li[i].className=''
        }
        img[index].className='show'
        li[index].className='active'
        index--
    }
    span[1].οnclick=function(){
        if(index==img.length){
            index=0
        }
        for(let i=0;i<img.length;i++){
            img[i].className=''
            li[i].className=''
        }
        img[index].className='show'
        li[index].className='active'
        index++
    }
    for(let i=0;i<li.length;i++){
        li[i].οnclick=function(){
            index=i
            for(let j=0;j<li.length;j++){
                li[j].className=''
                img[j].className=''
            }
            li[i].className='active'
            img[i].className='show'
        }
    }
    var timer=null
    function  autoPic(){
    timer=setInterval(function(){
        
        if(index==img.length){
            index=0
        }
        for(let i=0;i<img.length;i++){
            img[i].className=''
            li[i].className=''
        }
        img[index].className='show'
        li[index].className='active'
        index++
    },1000)
    }
     autoPic()
    ul.parentNode.οnmοuseοver=function(){
        clearInterval(timer)
    }
    ul.parentNode.onmouseout = ()=>{
        autoPic()
    }
    </script>
        
    </body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值