js淡入淡出轮播图 精华

<style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 600px;
            height: 400px;
            margin: 50px auto;
            border: 1px solid;
            position: relative;
        }
        .box .pic img{
            width: 600px;
            height: 400px;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
        }
        .box .pic .ac{
            opacity: 1;
            z-index: 1;
        }
        .box .btn{
            position: absolute;
            right: 20px;
            bottom: 20px;
            z-index: 9999;
        }
        .box .btn span{
            display: block;
            float: left;
            width: 30px;
            height: 30px;
            background-color:#ffffff;
            color:black;
            text-align: center;
            line-height: 30px;
            border-radius: 50%;
            margin: 4px;
            cursor: pointer;
        }
        .box .both{
            width: 100%;
            height: 5 0px;
            position: absolute;
            top: 50%;
            left: 0;
            margin-top: -25px;
            position: relative;
        }
        .box .both i{
            font-style: normal;
            font-size: 26px;
            position: absolute;
            width:50px;
            height:50px;
            text-align: center;
            line-height: 50px;
            background-color: rgba(200,200,200, .6);
            cursor: pointer;
            color: #ffffff;
            z-index: 9;
        }
        .box .both i:nth-child(1){
            left: 0;
        }
        .box .both i:nth-child(2){
            right: 0;
        }
        .bg{
            background-color: red !important;
        }
        .box .pic .pc{
            z-index: 1;
            opacity: 1;
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div class="box">
       <div class="pic">
           <img src="img2/1.jpg" alt="" class="ac">
           <img src="img2/2.jpg" alt="">
           <img src="img2/3.jpg" alt="">
           <img src="img2/4.jpg" alt="">
           <img src="img2/5.jpg" alt="">
       </div>
       <div class="btn">
           <span>1</span>
           <span>2</span>
           <span>3</span>
           <span>4</span>
           <span>5</span>
       </div>
       <div class="both">
           <i><<</i>
           <i>>></i>
       </div>
    </div>
    <script>
        var pics=document.querySelector(".pic")
        var pic=pics.children
        var btn=document.querySelectorAll("span")
        var both=document.querySelectorAll("i")
        var box=document.querySelector(".box")

        var index=0
        var timer=null
        btn[0].className="bg"
        for(var i=0;i<btn.length;i++){
            btn[i].index=i
            btn[i].onclick=function(){
                for(var j=0;j<btn.length;j++){
                    btn[j].className=""
                    pic[j].className=""
                }
                this.className="bg"
                pic[this.index].className="pc"
            }
        }
        both[0].onclick=function(){
            index--
            if(index<0){
            index=btn.length-1
            }
            for(var j=0;j<btn.length;j++){
                    btn[j].className=""
                    pic[j].className=""
                }
             btn[index].className="bg"
             pic[index].className="pc"
        }
        both[1].onclick=function(){
            index++
            if(index>pic.length-1){
            index=0
            }
            for(var j=0;j<btn.length;j++){
                    btn[j].className=""
                    pic[j].className=""
                }
             btn[index].className="bg"
             pic[index].className="pc"
        }
        timer=setInterval(function(){
            both[1].onclick()
        },3000)
        box.onmouseover=function(){
            clearInterval(timer)
        }
        box.onmouseout=function(){
          timer=setInterval(function(){
            both[1].onclick()
            },3000)
        }
    </script>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值