标签:
在做页面中,多数情况下都会遇到页面上做动画效果,之前一直都是用jquery,一直没有试过用原生的js来做,今天正好看到一篇js原生动画的效果,特记录在此,
原文地址:http://www.it165.net/pro/html/201410/23513.html
1、匀速动画效果
说明:匀速动画就是动画的效果从开始到结束每次执行的速度都是一致的
01.<!DOCTYPE html PUBLIC ‘-//W3C//DTD XHTML 1.0 Transitional//EN‘ ‘http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd‘> 02.<html xmlns=‘http://www.w3.org/1999/xhtml‘> 03.<head> 04.<meta http-equiv=‘Content-Type‘ content=‘text/html; charset=utf-8‘ /> 05.<title>匀速动画</title> 06.<style type=‘text/css‘> 07.html,body{margin:0;padding:0;} 08.div{margin:0;padding:0;} 09..odiv{width:200px; height:200px; background:#f00; position:relative; left:-200px; top:100px;} 10..sdiv{width:20px; height:60px; background:#00f; position:absolute; top:70px; right:-20px;} 11.</style> 12.</head> 13.<body> 14.<div id=‘odiv‘ class=‘odiv‘> 15.<div id=‘sdiv‘ class=‘sdiv‘> 16.</div> 17.</div> 18.</body> 19.</html> 20.<script language=‘javascript‘> 21.window.onload = function(){ 22.var odiv = document.getElementById(‘odiv‘); 23.odiv.onmouseover = function(){ 24.startMover(0); 25.} 26.odiv.onmouseout = function(){ 27.startMover(-200); 28.} 29.} 30.var timer = null; 31.function startMover(itarget){//目标值 32.clearInterval(timer);//执行当前动画同时清除之前的动画 33.var odiv = document.getElementById(‘odiv‘); 34.timer = setInterval(function(){ 35.var speed = 0; 36.if(odiv.offsetLeft > itarget){ 37.speed = -1; 38.} 39.else{ 40.speed = 1; 41.} 42.if(odiv.offsetLeft == itarget){ 43.clearInterval(timer); 44.} 45.else{ 46.odiv.style.left = odiv.offsetLeft+speed+‘px‘; 47.} 48.},30); 49.} 50.//注明:offsetWidth = width+padding+border 51.//offsetHeight = height+padding+border 52.//offsetWidth=(border-width)*2+(padding-left)+(width)+(padding-right) 53.//offsetHeight=(border-width)*2+(padding-top)+(height)+(padding-bottom) 54./* 55.offsetLeft=(offsetParent的padding-left)+(中间元素的offsetWidth)+(当前元素的margin-left)。 56.offsetTop=(offsetParent的padding-top)+(中间元素的offsetHeight)+(当前元素的margin-top)。 57.当offsetParent为body时情况比较特殊: 58.在IE8/9/10及Chrome中,offsetLeft = (body的margin-left)+(body的border-width)+(body的padding-left)+(当前元素的margin-left)。 59.在FireFox中,offsetLeft = (body的margin-left)+(body的padding-left)+(当前元素的margin-left)。 60.offsetParent属性返回一个对象的引用,这个对象是距离调用offsetParent的元素最近的(在包含层次中最靠近的),并且是已进行过CSS定位的容器元素。 如果这个容器元素未进行CSS定位, 则offsetParent属性的取值为根元素的引用。 61.总的来说两条规则: 62.1、如果当前元素的父级元素没有进行CSS定位(position为absolute或relative),offsetParent为body。 63.2、如果当前元素的父级元素中有CSS定位(position为absolute或relative),offsetParent取最近的那个父级元素。 64.*/ 65.</script>
2、缓冲动画
说明:缓冲动画就是动画到结束或这开始的时候,速度是随着动画执行的进度动态变化的
3、透明度动画
说明:处理元素透明效果的动画
4、多物体动画
说明:多个物体在一起执行的动画效果


5、获取样式动画
说明:这里的获取样式是通过计算出来元素的样式,然后通过这个计算出来的结果来操作元素


6、多物体复杂动画
说明:多物体复杂动画可以控制元素的不同属性变化来实现动画效果


7、多物体复杂动画(带透明度的)


01.<!DOCTYPE html PUBLIC ‘-//W3C//DTD XHTML 1.0 Transitional//EN‘ ‘http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd‘>02.<html xmlns=‘http://www.w3.org/1999/xhtml‘>03.<head>04.<meta http-equiv=‘Content-Type‘ content=‘text/html; charset=utf-8‘ />05.<title>多物体复杂动画(带透明度的)</title>06.<style type=‘text/css‘>07.body,div,dl,dt,dd,ul,ol,li,h3,h4,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,tr,td {margin:0;padding:0; font-size:12px;}08.table {border-collapse:collapse;border-spacing:0;}09.fieldset,img {border:0}10.address,caption,cite,code,dfn,em,strong,th,var {font-style:normal;font-weight:normal}11.ol,ul {list-style:none}12.caption,th,td{text-align:center}13.h3,h4,h3,h4,h5,h6 {font-size:100%;font-weight:normal}14.q:before,q:after {content:‘‘}15.abbr,acronym { border:0}16..odiv{position:relative;}17..odiv ul li{width:200px; height:100px; background:yellow; margin-bottom:20px; border:2px solid #000;}18.#li1{opacity:0.3;filter:alpha(opacity:30);}19.</style>20.</head>21. 22.<body>23.<div id=‘odiv‘ class=‘odiv‘>24.<ul>25.<li id=‘li1‘></li>26.<li id=‘li2‘></li>27.</ul>28.</div>29.</body>30.</html>31.<script language=‘javascript‘>32.window.onload = function(){33.var li1 = document.getElementById(‘li1‘);34.var li2 = document.getElementById(‘li2‘);35.li1.onmouseover = function(){36.startMov(this,100,‘opacity‘);37.};38.li1.onmouseout = function(){39.startMov(this,30,‘opacity‘);40.};41.li2.onmouseover = function(){42.startMov(this,200,‘height‘);43.};44.li2.onmouseout = function(){45.startMov(this,100,‘height‘);46.}47.li1.timer = null;48.li2.timer = null;49.function startMov(obj,itarget,attr){50.clearInterval(obj.timer);//执行动画之前清除动画51.obj.timer = setInterval(function(){52.var icur = 0;53.if(attr == ‘opacity‘){54.icur = Math.round(parseFloat(getStyle(obj,attr))*100);//转换成整数,并且四舍五入下55.//计算机在计算小数的时候往往是不准确的!56.}57.else{58.icur = parseInt(getStyle(obj,attr));59.}60.var speed =0;61.speed = (itarget - icur)/8;62.speed = speed>0?Math.ceil(speed):Math.floor(speed);63.if(icur == itarget){64.clearInterval(obj.timer);65.}66.else{67.if(attr == ‘opacity‘){68.obj.style.filter = ‘alpha(opacity:‘+(icur+speed)+‘)‘;69.obj.style.opacity = (icur+speed)/100;70.}71.else{72.obj.style[attr] = icur+speed+‘px‘;73.}74.}75.},30);76.}77.function getStyle(obj,attr)78.{79.if(obj.currentStyle){80.return obj.currentStyle[attr];81.}82.else{83.return getComputedStyle(obj,false)[attr];84.}85.}86.}87.//offsetWidth获取的是元素实际的宽度(包括边框和内边距)88.//只要是多物体运动,所有的属性都不能共用89.</script>8、链式动画
说明:链式动画就是当前动画执行完成后执行下一个动画效果


9、多物体同时运动动画(支持链式动画)
最后一个动画效果完善了上述所有动画的代码,自己可以根据上述的代码进行扩展!
标签:
原文地址:http://www.cnblogs.com/mopagunda/p/4953070.html