轮播图

5 篇文章 0 订阅
5 篇文章 0 订阅

原理

1. 轮播图主要是利用了 overflow:hidden 与 position:absolute;这两个属性
2. 通过overflow:hidden;隐藏图片,通过position:absolute的left或者right显示隐藏的图片

CSS

<style>
    *{
        margin: 0;
        padding: 0;
    }
    .wrapper{
        position: relative;    
        font-size:30px;
        text-align: center;
        line-height: 200px;    
        width:400px;
        height:200px;
        margin:100px;
        overflow: hidden;
    }
    ul{
        position: absolute;
        width:1600px;
        height:200px;
        padding: 0;
        list-style:none;        
    }
    li{
        width:400px;
        height:100%;
        float: left;
    }
    li:nth-child(1){
        background:orange; 
    }
    li:nth-child(2){
        background: aqua;
    }
    li:nth-child(3){
        background: chartreuse;
    }
    li:nth-child(4){
        background: hotpink;
    }
    .prev,.next{
        position: absolute;
        top:50%;
        transform: translateY(-50%);
        font-size:30px;
        text-align: center;
        line-height: 30px;
        width:30px;
        height:30px;
        background: #e0f0e9;
        border-radius:50%;
    }
    .prev{
        left:10px;
    }
    .next{
        right:10px;
    }
    .dots{
        position: absolute;
        left:50%;
        bottom: 15px;    
        transform: translateX(-50%);
    }
    .dots div{
        width:20px;
        height:20px;
        background:white;
        border-radius: 50%;
        float: left;
        margin: 0 5px;
    }
    .dots>div:nth-child(1){
        background:#f20c00;
    }
</style>

HTML

<div class="wrapper">
    <ul>
        <li>one</li>
        <li>two</li>
        <li>three</li>
        <li>four</li>
    </ul>
    <div class="prev"><</div>
    <div class="next">></div>
    <div class="dots">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</div>

JavaScript

 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
 <script> 
    $(function(){
        // 点击次数
        let init = 0;、
        // 存储left 的值
        let pos
        // 点击左侧按钮
        $('.prev').on('click',function(){
            // 如果图片到了尽头,不能再滚动
            if(init>0)
            init -=1;                
            pos = -init * 400 
            console.log(init)
            $('ul').css({
                left:`${pos}px`
            })
            $('.dots>div').eq(init).css({
                background:'#f20c00'
            })
            // 清除上一个的样式
            $('.dots>div').eq(init+1).css({
                background:'white'
            })
        })
        $('.next').on('click',function(){
            if(init<3)
            init +=1;
            pos = -init * 400 
            console.log(init)
            $('ul').css({
                left:`${pos}px`
            })
            $('.dots>div').eq(init).css({
                background:'#f20c00'
            })
            $('.dots>div').eq(init-1).css({
                background:'white'
            })
        })
    })
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值