用class构造函数实现轮播图效果

<style>
			* {
				padding: 0;
				margin: 0;
			}
			
			.banner {
				position: relative;
				width: 810px;
				height: 320px;
				margin: 50px auto;
				background: grey;
				overflow: hidden;
			}
			
			.img-list {
				list-style: none;
				position: absolute;
				left: 0;
				top: 0;
				width: 5240px;
				height: 320px;
			}
			
			.img-list li {
				float: left;
			}
			
			.banner-nav-list {
				list-style: none;
				position: absolute;
				right: 100px;
				bottom: 50px;
			}
			
			.banner-nav-list li {
				background: red;
				float: left;
				width: 20px;
				height: 20px;
				margin-right: 10px;
				cursor: pointer;
				text-align: center;
			}
			
			.banner-nav-list li.active {
				background: yellow;
			}
			
			.banner .toLeft {
				width: 60px;
				height: 60px;
				background: url(images/btn.gif);
				position: absolute;
				left: 30px;
				top: 120px;
				/*opacity: 0;*/
			}
			
			.banner .toRight {
				width: 60px;
				height: 60px;
				background: url(images/btn.gif) 0px 60px;
				position: absolute;
				right: 30px;
				top: 120px;
				/*opacity: 0;*/
			}
			
			#arr {
				opacity: 0;
			}
		</style>
	</head>

	<body>
		<div class="banner" id="banner">
			<ul class="img-list" id="img-list">
				<li index="0"><img src="images/001.jpg"></li>
				<li index="1"><img src="images/002.jpg"></li>
				<li index="2"><img src="images/003.jpg"></li>
				<li index="3"><img src="images/004.jpg"></li>
				<li index="0"><img src="images/001.jpg"></li>
			</ul>

			<ul class="banner-nav-list" id="banner-nav-list">
				<li class="active">1</li>
				<li>2</li>
				<li>3</li>
				<li>4</li>
			</ul>
			<div id="arr">
				<div class="toLeft" id="toLeft"></div>
				<div class="toRight" id="toRight"></div>
			</div>
		</div>
		<script src="./animate.5.0.js"></script>
		<script>
			/*
			 * 自动轮播
			 */
			
			//属性
			
			//方法
			
			//es6
			class AutoPlay{
				constructor(){
					this.imgList = document.getElementById("img-list");
					this.banner = document.getElementById("banner");
					this.arr = document.getElementById("arr");
					this.toLeft = document.getElementById("toLeft");
					this.toRight = document.getElementById("toRight");
					this.navList = document.getElementById("banner-nav-list");
					this.navLi = this.navList.children;
					this.imgLi = this.imgList.children;
					this.index = 0;
					this.timer = null;
				}
				auto(){
					this.timer = setInterval(this.indexChange.bind(this),1000);
					return this;
				}
				indexChange(){
					if(this.index === this.imgLi.length-1){
						this.imgList.style.left = 0;
						this.index = 1;
					}else{
						this.index++;
					}
					var j = this.index === this.imgLi.length-1 ? 0 : this.index;
					this.potoMove(j);
					
					return this;
				}
				potoMove(j){
					for (var i = 0; i < this.navLi.length; i++) {
						this.navLi[i].className = "";
					}
					animate(this.imgList,{left:-810 * this.index});
					this.navLi[j].className = "active";
					
					return this;
				}
                bannerOver(){
                    this.banner.onmouseover = function(){
					clearInterval(this.timer);
					animate(this.arr,{opacity:100});
                    }.bind(this);
                    
                    return this;
                }
                bannerOut(){
                    this.banner.onmouseout = function(){
					this.timer = setInterval(this.indexChange.bind(this),1000);
					animate(this.arr,{opacity:0});
                    }.bind(this);
                    console.log(this)
                    
				return this;
                }
                leftClick(){
                    var _this = this;
                    this.toLeft.onclick = function(){
                        if(_this.index === 4){
                            _this.index = 0;
                            _this.imgList.style.left = 0;
                        }else if(_this.index === 0){
                            _this.index = 0;
                        }else{
                            _this.index--;
                        }
                        _this.potoMove(_this.index);
                    }
                    console.log(1)
                    return this;
                }
                rightClick(){
                    var _this = this;
				    this.toRight.onclick = function(){
					if(_this.index === 4){
						_this.index = 1;
						_this.imgList.style.left = 0;
					}else if(_this.index === 3){
						_this.index = 3;
					}else{
						_this.index++;
					}
					_this.potoMove(_this.index);
				}
				
				return this;
                }
                init(){
                    
                    this.auto();
                    this.bannerOver();
                    this.bannerOut();
                    this.leftClick();
                    this.rightClick()
                    /* this.indexChange(); */
                    /* this.potoMove(); */
                }
			}
			new AutoPlay().init();
animate  js
function animate(obj,param,callBack,speedTime){
	//如果动画不需要 传递回调,但需要设置时间,这时speedTime是无效的?
	if(!!callBack && typeof callBack != "function"){//当callBack传递了参数,但这个参数又不是函数的时候,说明callBack是一个时候毫秒数,要将callBack交给speedTime
		speedTime = callBack;
		callBack = undefined;//这里要将callBack置为undefined,因为是一个值,后面的回调会运行到。
	}
	speedTime = speedTime || 10;
	clearInterval(obj.timer);//清除上一次当前对象的定时器,定时器私有化
	obj.timer = setInterval(function(){
		var isOver = true;//监听是否所有样式都到达 了目标 值,true:表示所有都到达了目标值
		for(var attr in param){
			if(attr != "zIndex"){
				var current = 0;//起点
				if(attr === "opacity"){
					current = getStyle(obj,attr) * 100;
				}else{
					current = parseInt(getStyle(obj,attr));
				}
				var speed = (param[attr] - current)/10;//速度
				speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
				if(current != param[attr]){//没到达目标值
					isOver = false;//监听器置为false
					if(attr === "opacity"){ //终点
						var filterVal = (current + speed)/100;
						setFilter(obj,filterVal);
					}else{
						obj.style[attr] = current + speed + "px";
					}
				}
			}else{
				obj.style[attr] = param[attr];
			}
		}
		if(isOver){//所有样式到达了目标值后,清除定时器
			clearInterval(obj.timer);
			//开启下一个动画 
			if(!!callBack){//当传递了回调函数时才需要来运行
				callBack();
			}
		}
	},speedTime);
}

//封装一个函数,功能是根据样式来获取对应的样式值
function getStyle(obj,attr){
	if(!!window.getComputedStyle){
		return window.getComputedStyle(obj,null)[attr];
	}
	switch(attr){
		case "opacity":
			return obj.currentStyle["filter"].replace(/[^0-9.]/g,"")/100;
	}
	return obj.currentStyle[attr];
}
//兼容ie8设置透明度
function setFilter(obj,val){
	if(!!window.getComputedStyle){//现代浏览器
		obj.style.opacity = val;
		return;
	}
	//ie8
	obj.style.filter = "alpha(opacity:" + val*100 + ");";
}

你可能感兴趣的:(用class构造函数实现轮播图效果)