javascript运动详解(3)

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> div {width:200px; height:200px; margin:20px; float:left; background:yellow; border:10px solid black; font-size:14px;} </style> <script> window.onload=function () { var oDiv1=document.getElementById('div1'); oDiv1.onmouseover=function (){startMove(this, 'height', 400);}; oDiv1.onmouseout=function (){startMove(this, 'height', 200);}; var oDiv2=document.getElementById('div2'); oDiv2.onmouseover=function (){startMove(this, 'width', 400);}; oDiv2.onmouseout=function (){startMove(this, 'width', 200);}; var oDiv3=document.getElementById('div3'); oDiv3.onmouseover=function (){startMove(this, 'fontSize', 50);}; oDiv3.onmouseout=function (){startMove(this, 'fontSize', 14);}; var oDiv4=document.getElementById('div4'); oDiv4.onmouseover=function (){startMove(this, 'borderWidth', 100);}; oDiv4.onmouseout=function (){startMove(this, 'borderWidth', 10);}; }; function getStyle(obj, name) { if(obj.currentStyle){return obj.currentStyle[name];} else{return getComputedStyle(obj, false)[name];} } function startMove(obj, attr, iTarget) { clearInterval(obj.timer); obj.timer=setInterval(function (){ var cur=parseInt(getStyle(obj, attr)); var speed=(iTarget-cur)/6; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(cur==iTarget) { clearInterval(obj.timer); } else { obj.style[attr]=cur+speed+'px'; } }, 30); } </script> </head> <body> <div>变高</div> <div>变宽</div> <div>safasfasd</div> <div></div> </body> </html>

效果如下:

javascript运动详解

透明度的任意值,需要做判断:

判断 var cur=0 if ( attr== 'opacity '){ cur=parseFloat ( getStyle ( obj, attr)) *100 }else{ } 设置样式判断 if ( attr== 'opacity '){ obj.style.fiIter = 'alpha ( opacity: '( cur+speed ) + ')' obj.style.opacity= ( cur+speed ) /100 }else{ }

5.链式运动

多出来的一个参数,只有传进去的时候才调用
鼠标移入变宽,结束之后弹出abc
先横向展开.再以向展开
鼠标移出,先变回不透明,变矮,变窄

三.封装运动框架(源码下载:https://github.com/jingwhale/jsmove/blob/master/move.js)

基于以上的分析与总结,封装运动框架move.js如下:

function getStyle(obj, name) { if(obj.currentStyle) { return obj.currentStyle[name]; } else { return getComputedStyle(obj, false)[name]; } } function startMove(obj, json, fnEnd) { clearInterval(obj.timer); obj.timer=setInterval(function (){ var bStop=true; //假设:所有值都已经到了 for(var attr in json) { var cur=0; if(attr=='opacity') { cur=Math.round(parseFloat(getStyle(obj, attr))*100); } else { cur=parseInt(getStyle(obj, attr)); } var speed=(json[attr]-cur)/6; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(cur!=json[attr]) bStop=false; if(attr=='opacity') { obj.style.filter='alpha(opacity:'+(cur+speed)+')'; obj.style.opacity=(cur+speed)/100; } else { obj.style[attr]=cur+speed+'px'; } } if(bStop) { clearInterval(obj.timer); if(fnEnd)fnEnd(); } }, 30); }

move.js运动框架基本满足现在网页上所有动画的需求(不包括css3)。

四.应用应用

1,完成如下效果:

javascript运动详解

js代码如下:

<script src="https://www.jb51.net/move.js"></script> <script> window.onload=function () { var oDiv=document.getElementById('play'); var aBtn=oDiv.getElementsByTagName('ol')[0].getElementsByTagName('li'); var oUl=oDiv.getElementsByTagName('ul')[0]; var now=0; for(var i=0;i<aBtn.length;i++) { aBtn[i].index=i; aBtn[i].onclick=function () { now=this.index; tab(); }; } function tab() { for(var i=0;i<aBtn.length;i++) { aBtn[i].className=''; } aBtn[now].className='active'; startMove(oUl, {top: -150*now}); } function next() { now++; if(now==aBtn.length){now=0;} tab(); } var timer=setInterval(next, 2000); oDiv.onmouseover=function (){clearInterval(timer);}; oDiv.onmouseout=function (){timer=setInterval(next, 2000);}; }; </script>

应用2,完成如下效果:

javascript运动详解

代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgddff.html