27--花式轮播图笔记

轮播图布局   

<style>
        *{
            margin: 0;
            padding: 0;
        }
        li{
            list-style: none;
        }
        .banner{
            position: relative;
            overflow: hidden;
            margin: auto;
            width: 1000px;
        }
        .pic{
            position: relative;
            width: 1000%;
            overflow: hidden;
        }
        .pic li{
            float: left;
            width: 10%;
        }
        .pic li img{
            display: block;
            width: 100%;
        }
        .btns{
            position: absolute;
            bottom: 50%;
            width: 100%;
            height: 0;
        }
        .btns span{
            float: left;
            width: 50px;
            line-height: 50px;
            text-align: center;
            font-family:"宋体";
            font-size: 30px;
            color:red;
            background-color: rgba(255,255,255,.5);
        }
        .btns span + span{
            float: right;
        }
        .pic-num{
            position: absolute;
            width: 100%;
            bottom:20px;
            font-size: 0;
            text-align: center;
        }
        .pic-num li{
            display: inline-block;
            width: 15px;
            height: 15px;
            margin: 0 8px;
            border-radius: 50%;
            background-color:blue;
        }
        .pic-num li.on{
             background-color:pink;
        }
        .pic-num li:hover{
            background-color:pink;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="banner">
        <div class="pic">
            <ul>
                <li><a href=""><img src="images/1.jpeg" alt=""></a></li>
                <li><a href=""><img src="images/2.jpg" alt=""></a></li>
                <li><a href=""><img src="images/3.jpg" alt=""></a></li>
                <li><a href=""><img src="images/4.jpg" alt=""></a></li>
            </ul>
        </div>
        <div class="btns">
            <span>&lt;</span>
            <span>&gt;</span>
        </div>
        <ul class="pic-num">
        
        </ul>
    </div>
</body>

生成小圆点的方法

  1. 是通过for循环生成
for(var i=0;i<_length;i++){
             oPicNum.innerHTML +="<li></li>";
        }
  1. 通过数组拼接生成
var opicNumLi =new Array(_length+1);
        oPicNum.innerHTML = opicNumLi.join("<li></li>");
        console.log(oPicNum);

当图片切换时小圆点的颜色发生改变

  1. 通过index改变,将小圆点的[索引]=【index】,切换类名改变状态
opicNumLi[index].className ="on";

当移动到后一张图时,前张图对应的小圆点颜色为原来未点击状态

  1. 重新定义变量 upper
upper=0;//重新定义变量
oBtns[1].onclick =function(){
                index++;
               
                //当它大于_length  就设置为0
                index%=_length;
                opicNumLi[upper].className ="";
                opicNumLi[index].className ="on";
                upper =index;//upper等于index-1
                move();
            }
  1. 在index加加减减之前设置未未点击状态
oBtns[0].onclick =function(){
                opicNumLi[index].className ="";
                index--;
                if(index<0){
                    index=_length-1;
                }
                // opicNumLi[upper].className ="";
                opicNumLi[index].className ="on";
                // upper =index;
                move();
            }

点击小圆点 就可以切换图片

  1. 这时需要给小圆点绑定事件
for(var i=0;i<_length;i++){
         var thatLi =document.createElement("li");
         thatLi.index =i;
         thatLi.onclick=function(){
             opicNumLi[index].className ="";//原来显示的图片小圆点颜色消失
             index=this.index;
             opicNumLi[index].className ="on";//点击的圆点状态为on
             move();
         }
        oPicNum.appendChild(thatLi);
    }
    var opicNumLi =oPicNum.querySelectorAll("li");
    opicNumLi[index].className ="on";

防止快速点击图片 报错

  1. 快速点击时 动画还没走完 就index在一直加加 而opicNumLi[index].className ="";这样就会报错 因为超过length长度

定义开关,只有动画走完开关才开, 在开的状态,index才能加加,动画才能执行

var_status =true;
 function move() {
            _status =false;//开关关闭
        animation(opic,{
            left : index * -_width
        },{
            duration : 600
        },function () {
            _status =true;//动画执行完才打开开关
            if (index>=_length){
                index=0;
                this.style.left =0;//因为直接设置css,速度是非常快的,肉眼看不到效果
            }
        })
    }
     oBtns[0].onclick =function(){
        if (_status){
            opicNumLi[index].className ="";
           index--;
            if(index<0){
                opic.style.left =-_length* _width+"px";
                index=_length-1;
            }
            opicNumLi[index].className ="on";
            move();
        }

    }
    oBtns[1].onclick =function(){
        if (_status){
            opicNumLi[index].className ="";
            index++;
            opicNumLi[index%_length].className ="on"; //当它大于_length  就设置为0
            move();
        }

    }

具体代码https://github.com/LXhby/6.5.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值