轮播图

1) Add HTML

<div id="slider-bar">
    <div id="imgs-box">
        <ul>
            <li><img src="https://i.imgur.com/4Fk8z2L.jpg" alt=""></li>
            <li><img src="https://i.imgur.com/C0e0zms.jpg" alt=""></li>
            <li><img src="https://i.imgur.com/24Ri2FT.jpg" alt=""></li>
            <li><img src="https://i.imgur.com/tmLQ3r7.jpg" alt=""></li>
        </ul>
    </div>
    <div id="indicators-box">
        <ol>
            <li style="background-color: #a10000"></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
    </div>
    <div id="control-box">
        <span class="left">上一张</span>
        <span class="right">下一张</span>
    </div>
</div>

2) Add CSS

#slider-bar {
    margin: 100px auto;
    width: 750px;
    height: 281px;
    position: relative;
    overflow: hidden;
}

#imgs-box ul li {
    width: 750px;
    height: 281px;
    position: absolute;
    left: 0;
    top: 0;
}

#imgs-box ul li:first-child {
    display: block;
}

#slider-bar ol {
    position: absolute;
    right: 10px;
    bottom: 10px;
}

#slider-bar ol li {
    width: 20px;
    height: 20px;
    background-color: darkgray;
    display: inline-block;
    margin-right: 3px;
    border-radius: 50%;
    cursor: pointer;
}

#slider-bar ol li.current {
    background-color: orangered;
}

#control-box span {
    cursor: pointer;
    text-indent: -999em;
    position: absolute;
    width: 41px;
    height: 69px;
    top: 90px;
}

#control-box span.left {
    background: url(https://i.imgur.com/LpPhS8J.png) no-repeat -84px 50%;
    left: 20px;
}

#control-box span.right {
    background: url(https://i.imgur.com/LpPhS8J.png) no-repeat -125px 50%;
    right: 20px;
}

#control-box span.left:hover {
    background: url(https://i.imgur.com/LpPhS8J.png);
}

#control-box span.right:hover {
    background: url(https://i.imgur.com/LpPhS8J.png) no-repeat -43px 50%;
}

3) Add jQuery

    var c = 0;
    // 1 自动轮播
    var timer = setInterval((function () {
        c++;
        c = c == 4 ? 0 : c;
        $("#imgs-box ul li").eq(c).fadeIn(300).siblings('li').fadeOut(300);
        $("#indicators-box ol li").eq(c).css('background', '#a10000').siblings('li').css('background', '#ddd');
    }), 2000);
    // 2 鼠标移入/移处轮播图时
    $("#slider-bar").hover(function () {
        clearInterval(timer);
    }, function () {
        timer = setInterval((function () {
            c++;
            c = c == 4 ? 0 : c;
            $("#imgs-box ul li").eq(c).fadeIn(300).siblings('li').fadeOut(300);
            $("#indicators-box ol li").eq(c).css('background', '#a10000').siblings('li').css('background', '#ddd');
        }), 1000);
    });
    // 3 鼠标进入轮播图片指针时
    $("#indicators-box ol li").mouseenter(function () {
        c = $(this).index();
        $("#imgs-box ul  li").eq(c).fadeIn(300).siblings('li').fadeOut(300);
        $("#indicators-box ol li").eq(c).css('background', '#a10000').siblings('li').css('background', '#ddd');
    })
    // 4 鼠标点击上一张按钮时
    $("#control-box .left").click(function () {
        c -= 1;
        c = c == -1 ? 3 : c;
        $("#imgs-box ul li").eq(c).fadeIn(300).siblings('li').fadeOut(300);
        $("#indicators-box ol li").eq(c).css('background', '#a10000').siblings('li').css('background', '#ddd');
    })
    // 5 鼠标点击下一张按钮时
    $("#control-box .right").click(function () {
        c += 1;
        c = c == 4 ? 0 : c;
        $("#imgs-box ul li").eq(c).fadeIn(300).siblings('li').fadeOut(300);
        $("#indicators-box ol li").eq(c).css('background', '#a10000').siblings('li').css('background', '#ddd');
    })

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值