轮播图系列教程(五),左右切换,运动框架应用——和派孔明

<span style="font-size:18px;"><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>幻灯片左右滑动效果 - 和派孔明 高级前端工程师</title>
<link href="css.css" rel="stylesheet" type="text/css" /></span><pre name="code" class="css"><strong><style>
* { padding: 0; margin: 0; }
li { list-style: none; }
img { border: none; }

body { background: #ecfaff; }

.box { width: 470px; height: 150px; overflow: hidden; position: relative; margin: 150px auto 0; background: #ccc; }
.box ol { position: absolute; left: 200px; bottom: 5px; z-index: 2; }
.box ol li { float: left; margin-right: 3px; display: inline; cursor: pointer; background: #fcf2cf; border: 1px solid #f47500; padding: 2px 6px; color: #d94b01; font-family: arial; font-size: 12px; }
.box .active { padding: 3px 8px; font-weight: bold; color: #ffffff; background: #ffb442; position: relative; bottom: 2px; }

.box ul { position: absolute; top: 0; left: 0; z-index: 1; }
.box ul li { width: 470px; height: 150px; float: left; }
.box ul img { float: left; width: 470px; height: 150px; }

.box p { width: 20px; height: 20px; line-height: 20px; font-family: arial; color: #fff; text-align: center; font-weight: bold; background: #333; border: 1px solid #ccc; position: absolute; top: 60px; z-index: 2; cursor: pointer; }
.box .prev { left: 10px; }
.box .next { right: 10px; }
</style></strong><pre name="code" class="javascript">
 <script> 
//简易运动框架
/*-----------------------------*/
<strong>function move(obj,json,opational){
	
	opational=opational||{};
	opational.time=opational.time||300;
	opational.fn=opational.fn||null;
	opational.type=opational.type||'ease-out';
	
	var start={};
	var dis={};
	for(var key in json){
		start[key]=parseFloat(getStyle(obj,key));
		dis[key]=json[key]-start[key];
	}
	
	var count=Math.round(opational.time/30);
	var n=0;
	
	clearInterval(obj.timer);
	obj.timer=setInterval(function(){
		
		n++;
		
		for(var key in json){
			
			//var cur=start[key]+n*dis[key]/count;
			switch(opational.type){
				case 'linear':
					var a=n/count;
					var cur=start[key]+dis[key]*a;
					break;	
				case 'ease-in':
					var a=n/count;
					var cur=start[key]+dis[key]*a*a*a;
					break;	
				case 'ease-out':
					var a=1-n/count;
					var cur=start[key]+dis[key]*(1-a*a*a);
					break;	
			}
		
		
			if(key=='opacity'){
				obj.style.opacity=cur;
				obj.style.filter='alpha(opacity='+cur*100+')';
			}else{
				obj.style[key]=cur+'px';
			}
		}
		
		console.log('跑');
		
		if(n==count){
			clearInterval(obj.timer);
			opational.fn && opational.fn();
		}
		
	},30);	
}

function getStyle(obj,attr){
	return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,false)[attr];	
}</strong>
/*-----------------------------*/

window.οnlοad=function(){ var oPlay=document.getElementById('play'); var aLi=oPlay.children[2].children; var oUl=oPlay.children[3]; var oNext=oPlay.getElementsByClassName('next')[0]; var oPrev=oPlay.getElementsByClassName('prev')[0]; var now=0; var ready=true; oUl.innerHTML+=oUl.innerHTML; oUl.style.width=oUl.children.length*oUl.children[0].offsetWidth+'px'; for(var i=0;i<aLi.length;i++){ (function(index){ aLi[i].οnclick=function(){ now=index; tab(); }; })(i); } function tab(){ for (var i = 0; i < aLi.length; i++) { aLi[i].className=''; } if(now==5){ aLi[0].className='active'; }else{ aLi[now].className='active'; } move(oUl,{left:-now*oUl.children[0].offsetWidth},{time:700, fn:function(){ raady=true; if(now==5){ //归位 oUl.style.left=0; now=0; } }}); } oNext.οnclick=function(){ if(!ready) return; now++; tab(); }; oPrev.οnclick=function(){ if(!ready) return; ready=false; now--; if(now==-1){ now=4; oUl.style.left= -oUl.offsetWidth/2+'px'; } tab(); };};</script></head><body><div class="box" id="play"> <p class="prev">«</p> <p class="next">»</p> <ol> <li class="active">1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ol> <ul> <li><a href="http://javascript"><img src="images/1.jpg" alt="和派孔明" /></a></li>
<li><a href="http://javascript"><img src="images/2.jpg" alt="和派孔明" /></a></li> <li><a href="http://javascript"><img src="images/3.jpg" alt="和派孔明" /></a></li>
<li><a href="http://javascript"><img src="images/4.jpg" alt="和派孔明" /></a></li> <li><a href="http://javascript"><img src="images/5.jpg" alt="和派孔明" /></a></li> </ul></div>
</body></html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值