本文实例讲述了纯js实现可用于页码更换的飞页特效。分享给大家供大家参考,具体如下:
这个效果使用了自己封装的一个运动函数;这个效果的巧妙之处在于,在开始用数组存放了每个li的位置信息,然后在点击按钮(页码)的时候让li的宽高位置和透明度发生运动的改变一个一个的消失,然后在消失结束之后,再一个个倒着出现;可以和页码进行匹配,从而实现页码更换的效果
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>www.jb51.net js飞入效果</title> <link rel="stylesheet" href="stylesheets/base.css" rel="external nofollow" > <style> body{ background:#000; } .header{ width: 100%; height: 40px; background:#fff; font:bold 30px/40px '微软雅黑'; text-align:center; } input{ background:#fff; margin-top:5px; width: 50px; height: 20px; font:bold 12px/20px '微软雅黑'; } ul{ width: 360px; height: 360px; margin:50px auto; } ul li{ width: 100px; height: 100px; background:skyblue; float:left; margin:5px; } </style> <script src="move.js"></script> <script> window.onload=function(){ var obtn=document.getelementbyid('btn1'); var oul=document.getelementsbytagname('ul')[0]; var ali=oul.children; //用数组来存放没个li的位置 var arr=[]; //存位置 for(var i=0;i<ali.length;i++) { arr[i] = { left:ali[i].offsetleft, top:ali[i].offsettop }; } //给目前的li定位 for(var i=0;i<arr.length;i++){ ali[i].style.position='absolute'; ali[i].style.left=arr[i].left+'px'; ali[i].style.top=arr[i].top+'px'; ali[i].style.margin=0; } //当点击的时候开定时器,让li一个一个的走 var inow=0; var timer=null; var bready=false; obtn.onclick=function(){ if(bready){return;} bready=true; timer=setinterval(function(){ move(ali[inow],{left:0,top:0,height:0,width:0,opacity:0},{'duration':200,'complete':function(){ if(inow==arr.length-1){ clearinterval(timer); back(); bready=false; } inow++; }}); },220); }; function back(){ timer=setinterval(function(){ inow--; move(ali[inow],{left:arr[inow].left,top:arr[inow].top,height:100,width:100,opacity:1},{'duration':200,'complete':function(){ if(inow==0){ clearinterval(timer); } }}); },220); } }; </script> </head> <body> <div class="header">飞页效果</div> <input type="button" value="走你" id="btn1"> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </body> </html>
运动函数move.js:
/** * created by jason on 2016/11/7. */ function getstyle(obj,sname){ return (obj.currentstyle || getcomputedstyle(obj,false))[sname]; } function move(obj,json,options){ var options=options || {}; var duration=options.duration || 1000; var easing=options.easing || 'linear'; var start={}; var dis={}; for(name in json){ start[name]=parsefloat(getstyle(obj,name)); dis[name]=json[name]-start[name]; } //start {width:50,} dis {width:150} //console.log(start,dis); var count=math.floor(duration/30); var n=0; clearinterval(obj.timer); obj.timer=setinterval(function(){ n++; for(name in json){ switch (easing){ case 'linear': var a=n/count; var cur=start[name]+dis[name]*a; break; case 'ease-in': var a=n/count; var cur=start[name]+dis[name]*a*a*a; break; case 'ease-out': var a=1-n/count; var cur=start[name]+dis[name]*(1-a*a*a); break; } if(name=='opacity'){ obj.style.opacity=cur; }else{ obj.style[name]=cur+'px'; } } if(n==count){ clearinterval(obj.timer); options.complete && options.complete(); } },30); }
运行效果如下:
更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript运动效果与技巧汇总》、《javascript动画特效与技巧汇总》、《javascript图形绘制技巧总结》、《javascript切换特效与技巧总结》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结》
希望本文所述对大家javascript程序设计有所帮助。
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论