《js动画效果》之链式运动

来源:互联网 发布:bt下载软件苹果 编辑:程序博客网 时间:2024/05/21 14:41


js

学习视频资源来自慕课网《js动画效果》:http://www.imooc.com/learn/167。

根据视频所讲,所谓链式运动,即多个动画效果依次发生,eg: 让一个物体先变宽,然后变高。这节课程的代码实现建立在前几节课程的基础上,要实现链式运动的关键点是回调函数的使用,下面的重点即是如何实现回调。练习代码如下:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2.  <html>  
  3.     <head>  
  4.         <meta charset="utf-8">  
  5.         <style type="text/css">  
  6.         *{  
  7.             padding:0;  
  8.             margin:0;  
  9.         }  
  10.         ul li{  
  11.             list-style:none;  
  12.             width:200px;  
  13.             height:100px;  
  14.             background:green;  
  15.             margin:20px;  
  16.             border:2px #ccc solid;  
  17.             filter:alpha(opacity:30);  
  18.             opacity:0.3;  
  19.         }  
  20.         </style>  
  21.         <script type="text/javascript">  
  22.             window.onload = function(){  
  23.                 var testLi = document.getElementById("testLi");  
  24.                 testLi.onmouseover = function(){  
  25.                     playFun(testLi, 300, "height", function(){//回调  
  26.                         playFun(testLi, 400, "width", function(){//回调  
  27.                             playFun(testLi, 100, "opacity");  
  28.                         });  
  29.                     });</span>  
  30.                 };  
  31.                 testLi.onmouseout = function(){  
  32.                     playFun(testLi, 30, "opacity", function(){//回调  
  33.                         playFun(testLi, 200, "width", function(){//回调  
  34.                             playFun(testLi, 100, "height");  
  35.                         });  
  36.                     });</span>  
  37.                 }  
  38.   
  39.                 function playFun(obj,itarget,attr,fn){//fn——回调函数  
  40.                     clearInterval(obj.timer);  
  41.                     obj.timer = setInterval(function(){  
  42.                         var getValue = 0;  
  43.                         if(attr == "opacity"){  
  44.   
  45.                             //parseFloat返回小数值  
  46.                             //由于计算机存储小数有误差,采用Math.round()四舍五入得整数  
  47.                             getValue = Math.round(parseFloat(getStle(obj,attr))*100);  
  48.                         }else{  
  49.                             getValue = parseInt(getStle(obj,attr));  
  50.                         }  
  51.                           
  52.                         var speed = (itarget - getValue)/10;  
  53.                         speed = speed>0?Math.ceil(speed):Math.floor(speed);  
  54.                           
  55.                         //判断运动是否结束  
  56.                         if(getValue == itarget){  
  57.                             clearInterval(obj.timer);  
  58.                             if(fn){//动画结束后判断是否调用回调函数,存在即执行  
  59.                                 fn();  
  60.                             }  
  61.                         }else{  
  62.                             //obj.style[attr],采用中括号传参  
  63.                             if(attr == "opacity"){  
  64.                                 getValue += speed;  
  65.                                 obj.style["filter"] = "alpha(opacity:"+ getValue +")";  
  66.                                 obj.style["opacity"] = getValue/100;  
  67.                             }else{  
  68.                                 obj.style[attr] = getValue + speed + "px";  
  69.                             }  
  70.                               
  71.                         }  
  72.                     },50);  
  73.                 }  
  74.   
  75.                 //获取属性值  
  76.                 function getStle(ele,attr){   
  77.                     if(ele.currentStyle){ //兼容IE浏览器  
  78.                         return ele.currentStyle[attr];  
  79.                     }else{  //兼容firefox浏览器  
  80.                         return getComputedStyle(ele,false)[attr];  
  81.                     }  
  82.                 }  
  83.             }  
  84.         </script>  
  85.     </head>  
  86.     <body>  
  87.         <ul>  
  88.             <li id="testLi"></li>  
  89.         </ul>  
  90.     </body>  
  91.  </html> 
0 0
原创粉丝点击