js实现轮播图

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			* {
				padding: 0;
				margin: 0;
			}

			#oImg {
				width: 800px;
				height: 500px;
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				margin: auto;
				background: url(img/q0.png) no-repeat;
				background-size: 800px 500px;
			}

			ul {
				list-style: none;
				position: absolute;
				margin-top: 450px;
				margin-left: 280px;
			}

			li {
				width: 20px;
				height: 20px;
				background: white;
				border-radius: 50%;
				float: left;
				margin: 0 20px;
			}

			button {
				width: 50px;
				height: 50px;
				font-size: 50px;
				border: 0;
				background: white;
			}

			#btn1 {
				float: left;
				margin-top: 200px;
			}

			#btn2 {
				margin-top: 200px;
				float: right;
			}
		</style>
	</head>
	<body>
		<div id="oImg">
			<ul>
				<li></li>
				<li></li>
				<li></li>
				<li></li>
			</ul>
			<button type="button" id="btn1"><</button>
			<button type="button" id="btn2">></button>
		</div>
	</body>
</html>
<script type="text/javascript">
	class Slideshow{
		constructor(newImg,newLi,newOimg) {
		    this.img = newImg;
			this.oLi = newLi;
			this.oImg = newOimg;
			this.index = 0;
			this.oLi[this.index].style.background = "red";
			this.oImg.style.background="url(img/q"+this.index+".png)";
		}
		
		//设置li的背景颜色
		setLiColor(){
			for(let i=0; i<this.oLi.length; i++){
				if(i == this.index){
					this.oLi[i].style.background = "red";
				}else{
					this.oLi[i].style.background = "black";
				}
			}
		}
		//设置div的背景图片
		
		 setOimgBackGround(){
			this.oImg.style.background="url(img/q"+this.index+".png)"+"100%"+"100%";
		}
		//下一张图
		nextImg(){
			this.index++;
			if(this.index == this.oLi.length){
				this.index=0
			}
			this.setLiColor();
			this.setOimgBackGround();
		}
 		//上一张图
 		
		previousImg(){
			this.index--;
			if(this.index == -1){
				this.index = 3;
			}
			this.setLiColor();
			this.setOimgBackGround();
		}
 		
		eventBindBtn(){
			let btn1 = document.getElementById("btn1");
            let btn2 = document.getElementById("btn2");
            let that = this;
			btn1.onclick=function(){
				that.previousImg();
			}
			btn2.onclick=function(){
				that.nextImg();
			}
		}
		
		eventBindLi(){
			for(let i=0; i<this.oLi.length; i++){
				let that = this;
				this.oLi[i].onclick=function(){
					that.index = i;
					that.setLiColor();
					that.setOimgBackGround();
				}
			} 
		}
	}
	let Oli = document.getElementsByTagName("li");
    let oImg = document.getElementById("oImg");
    let sl = new Slideshow(["q0.png","q1.png","q2","q3.png"],Oli,oImg);
	sl.eventBindBtn();
	sl.eventBindLi(); 
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值