面向对象写一个轮播图(透明度的轮播图)

主要的代码内容:
function Slider(domID){
this.sliderBox = document.getElementById(domID);
this.slider = this.sliderBox.children[0];
this.sliderList = this.slider.children;

this.timer = null;
this.i = 0;
this.clear();
this.autoPlay();

}

Slider.prototype.move = function(){
this.i++;

	if(this.i == this.sliderList.length){
		this.i = 0;
	}
	if(this.i == -1){
		this.i = this.sliderList.length - 1;
	}
	
	if(this.numList){
		for(let i = 0; i < this.numList.length; i++){
			this.numList[i].className =  "";
		}
		this.numList[this.i].className = "hover";
	}
	
	for(let i = 0; i < this.sliderList.length; i++){
		startMove(this.sliderList[i],{opacity:0});
	}
	startMove(this.sliderList[this.i],{opacity:100});

}

Slider.prototype.autoPlay = function(){
this.timer = setInterval(()=>{
this.move();
},3000);
}
Slider.prototype.addBtns = function(){
let oBtn = document.createElement(“div”);
oBtn.className = “btn”;
this.sliderBox.appendChild(oBtn);
oBtn.innerHTML = “<>”;

let aBtns = oBtn.children;
aBtns[0].onclick = ()=>{
	this.i -= 2;
	this.move();
}
aBtns[1].onclick = ()=>{
	this.move();
}

}
Slider.prototype.addFocus = function(){
var oFocus = document.createElement(“ul”);
oFocus.className = “focus”;
this.sliderBox.appendChild(oFocus);
let str = “”;
for(let i = 0; i < this.sliderList.length; i++){
str += “

  • ”+(i+1)+"
  • "
    }
    oFocus.innerHTML = str;

    this.numList = oFocus.children;
    this.numList[0].className = "hover";
    
    for(let i = 0; i < this.numList.length; i++){
    	this.numList[i].onmouseover = ()=>{
    		this.i = i-1;
    		this.move();
    	}
    }
    

    }

    Slider.prototype.clear = function(){
    this.sliderBox.onmouseover = ()=>{
    clearInterval(this.timer);
    }
    this.sliderBox.onmouseout = ()=>{
    this.timer = setInterval(()=>{
    this.move();
    },3000)
    }
    }

    let slider = new Slider(“sliderBox”);
    slider.addBtns();
    slider.addFocus();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值