轮播图jq

 img{
            display: block;
        }
        li{
            list-style: none;
        }
        .box{
            width: 800px;
            margin: 50px auto;
            display: flex;
            position: relative;
        }
        .box1-1{
            width: 800px;
            /* overflow: hidden; */
            display: flex;
            overflow: hidden;
        }
        .box1-1 > img{
            width: 100%;
            position: relative;
        }
        
        .left ,.right{
            background: rgb(0, 0, 0, 0.3);
            width: 50px;
            height: 100px;
            font-size: 50px;
            font-weight: normal;
            text-align: center;
            color: rgb(255, 255,255,.3);
            line-height: 90px;
            transition: background-color .3s;
            cursor: pointer;
        }
        .left:hover ,.right:hover{
            color: #fff;
            background: rgb(0, 0,0,.5);
        }
        .left{
            position: absolute;
            left: 0;
            top: 40%;
        }
        .right{
            position: absolute;
            right: 0;
            top: 40%;
        }
        .dot {
            width: 800px;
            height: 100px;
            cursor: pointer;
        }
        .dot > ul{
            position: absolute;
            left: 45%;
            bottom: 5%;
        }
        .dot > ul > li{
            width: 10px;
            height: 10px;
            border-radius: 15px;
            background: #ffffff93;
            margin-right: 20px;
            float: left;
        }
        .active{
            background: #fff  !important;
        }

<div class="box">
        <!-- 图片 -->
        <div class="box1">
            <div class="box1-1">
                <img src="./imgs/f1.jpg" alt="">
                <img src="./imgs/f2.jpg" alt="">
                <img src="./imgs/f3.jpg" alt="">
                <img src="./imgs/f4.jpg" alt="">
            </div>
            <div class="arrow">
                <div class="left">&lt;</div>
                <div class="right">&gt;</div>
            </div>
        </div>
        <!-- 箭头 -->
        <!-- 圆点 -->
        <div class="dot">
            <ul>
                <li class=" d active"></li>
                <li class="d"></li>
                <li class="d"></li>
                <li class="d"></li>
            </ul>
        </div>
    </div>

 var imgw = $('.box1-1 img').innerWidth  //获取img的宽度
        
        var n = 0
          time = setInterval(function(){
                auto()
               
            },2000)

        // 轮播效果
        function auto(){
            $('.box1-1').animate({left:-imgw},function(){
                // img移到左边后排到这个队列的最后
                $(this).css('left',0).find('img:first').appendTo(this)   //这个img的左边为0,并且添加到当前队列
                console.log($(this));
            })
            n++;
            if(n > 3){
                n = 0
            }
            $('.d').eq(n).addClass('active').siblings('.d').removeClass('active')
        }
       
        // 鼠标移到box上时,轮播暂停
        $('.box1').hover(function(){
            clearInterval(time)
        },function(){
            time = setInterval(function(){
                auto()  
            },2000)
        })

        // 左右箭头
        $('.right').click(function(){
            auto()
        })

        $('.left').click(function(){
            // 1.现将img向左移除一个img的宽度,同时,将显示在最后一个img追加到box1-1最前面
            // 2.执行动画img的left为0最后一个img带回
            $('.box1-1').css('left',-imgw).find('img:last').prependTo('.box1-1')
            $('.box1-1').animate({left:0})
            n--
            if(n < 0){
                n = 3
            }
            $('.d').eq(n).addClass('active').siblings('.d').removeClass('active')
        })
       
        // 点击小圆点
        $('.dot ul li').each(function(index){
            // console.log(this);
            $(this).click(function(){
                if(n < index){
                    for(var i = n ;i <index;i++){
                            $('.box1-1').animate({left:-imgw},function(){
                            $(this).css('left',0).find('img:first').appendTo(this)   
                        })
                    }
                }else if(n > index){
                    for(var i = n ;i > index;i--){
                        $('.box1-1').css('left',-imgw).find('img:last').prependTo('.box1-1')
                        $('.box1-1').animate({left:0})
                    }
                }
                n = index
                $('.d').eq(n).addClass('active').siblings('.d').removeClass('active')
            })
        })

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值