jquery做轮播图

<body>
    <div class="contain">
        <ul class="contain_inner">
            <li class="item"><img src="../public/QQ截图20190419091559.jpg"/>0</li>
            <li class="item"><img src="../public/QQ截图20190419091610.jpg"/>1</li>
            <li class="item"><img src="../public/QQ截图20190419091625.jpg"/>2</li>
        </ul>
        <button class="btn_l"><</button>
        <button class="btn_r">></button>
    </div>
</body>
.contain {
        margin-left: 500px;
        position: relative;
    }
    .contain_inner {
        border: 1px solid red;
        list-style: none;
        position: relative;
        width: 100px;
        height: 100px;
        overflow: hidden;
        padding: 0;
    }
    .contain_inner > .item {
        position: absolute;
        left: 0;
        top: 0;
    }
    .btn_l,.btn_r {
        width: 15px;
        height: 15px;
        position: absolute;
        bottom: 35px;
    }
    .btn_l {
        left: 10px;
    }
    .btn_r {
        left: 75px;
    }
<script>
    // 图片变化
    function changeImg(index) {
        $('.item').eq(index).fadeIn().siblings().fadeOut()
    }

    // 自动变化
    var index = 0;
    changeImg(0);

    function autoPlay() {
        timer = setInterval(function () {
            index++;
            if (index === 3) {
                index = 0
            }
            changeImg(index)
            console.log(index);
        },1500)
    }
    autoPlay();
    // 手动切换
    $('.btn_r').click(function () {
        index++;
        if (index === 3) {
            index = 0
        }
        changeImg(index)
        console.log(index);
    })
    // 停止事件
    $('.contain_inner,.btn_r').mouseenter(function () {
        clearInterval(timer);
    }).mouseleave(function () {
        autoPlay()
    })
</script>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值