简易轮播图实现

css代码:

#banner{
    width: 700px;
    height: 280px;
    margin: 50px auto;
    background-size: 700px 280px;
    position: relative;
}
#banner span{
    display: inline-block;
    width: 40px;
    height: 70px;
    background:rgba(0,0,0,0.3);
    position: absolute;
    top:105px;
    display: flex;
    align-items: center;
}
#banner #left{
    left: 20px;
}
#banner #right{
    right: 20px;
}
#banner span em{
    display: inline-block;
    width: 20px;
    height: 20px;
    border-left: 5px solid white;
    border-bottom: 5px solid white;
}
#banner #left em{
    transform: rotate(48deg);
    margin-left: 13px;
}
#banner #right em{
    transform: rotate(-138deg);
}
#banner ul{
    width: 700px;
    height: 30px;
    position: absolute;
    bottom:0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    list-style: none;
    padding: 0;
    margin: 0;
}
#banner ul li{
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin: 0 10px;
    background:black;
}

html代码:

<div id="banner">
    <span id="left"><em></em></span>
    <span id="right"><em></em></span>
    <ul>
         <li></li>
         <li></li>
         <li></li>
         <li></li>
         <li></li>
    </ul>
</div>

js代码:

class banners{
    constructor(ops){
        this.banner = ops.banner;
        this.li = ops.li;
        this.left = ops.left;
        this.right = ops.right;
        this.index=0;
        this.arr = ops.arr;
        this.autoTime = ops.autoTime;
        this.first();
        this.addEvent();
        this.autoPlay();
    }
    //第一次的默认效果
    first(){
       this.setImg();
       this.setLi();
    }
    // 左按钮
    leftEvent(){
        this.index--;
        if(this.index==-1){
            this.index=this.arr.length-1
        }
        this.setImg();
        this.setLi();
        } 
    // 右按钮
    rightEvent(){
        this.index++;
        if(this.index==this.arr.length){
            this.index=0;
        }
         this.setImg();
         this.setLi();
   }
       //设置背景图片
    setImg(){      
              // 设置轮播大框的图片
       this.banner.style.backgroundImage=`url(${this.arr[this.index]})`
       }
    // li的颜色

    setLi(){
        for(let i = 0;i<this.li.length; i++){
            if(this.index == i){   //设置li的颜色,点击的li变色其他不变
                this.li[i].style.backgroundColor="red";
            }else{
               this.li[i].style.backgroundColor="black";
            }
        }
    }
    addEvent(){
        let that = this;
        this.left.onclick=function(){
            that.leftEvent();  //左按钮点击事件
        }
        this.right.onclick=function(){
            that.rightEvent();  //右按钮点击事件
        }
        for(let i=0;i<this.li.length;i++){
            this.li[i].onclick = function(){
               that.index=i;    //li的点击事件
               that.setImg();
               that.setLi();
            }
        } 
      
    }
   autoPlay(){ // 自动轮播
       if(!this.autoTime){
           this.autoTime = 3000;
       }
      var time = setInterval(()=>{
           this.rightEvent();
       }, this.autoTime);
      this.banner.onmouseover=function(){
           clearInterval(time); //鼠标进入清除计时器
      }
      var that = this;
      this.banner.onmouseout = function(){
           time = setInterval(()=>{  //鼠标离开计时器再次启动
               that.rightEvent();
           }, that.autoTime);
       }
   }
}
new banners({
   banner : document.querySelector("#banner"),
   li : document.querySelectorAll("ul li"),
   left : document.getElementById("left"),
   right : document.getElementById("right"),
   autoTime:1000,
   arr:[
        "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2067813354,2278267070&fm=26&gp=0.jpg",
        "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1742886454,2307269776&fm=26&gp=0.jpg",
        "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=249678191,2118772918&fm=26&gp=0.jpg",
        "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3596650269,3692947032&fm=26&gp=0.jpg",
        "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=357420862,1595125305&fm=26&gp=0.jpg"
             ]   
     })
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值