搞不懂哪里错了。。。
help~
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自主封装多功能动画框架</title> <style> ul li{ width: 300px;; height: 200px; background: pink; opacity: 0.3; } </style> <script> window.onload = function(){ // test var Li1 = document.getElementById('li1'); Li1.onmousemove = function(){ DIY_Animation(this,'opacity',100); } Li1.onmouseout = function(){ DIY_Animation(this,'opacity',30); } } //获取对象obj的attr属性 function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr];}// 有错! else{ return getComputedStyle(obj,false)[attr];} } //可以改变宽高、背景属性、字号属性、边框属性等等 //透明度(opacity)特殊处理,0~100为标准 // var alpha=30; function DIY_Animation(obj,attr,iTarget){ //obj=对象;attr=属性;iTarget=改变的数值 clearInterval(obj.timer); obj.timer = setInterval(function(){ var icur = 0; if(attr == 'opacity'){ icur = Math.round(parseFloat(getStyle(obj.attr))*100); }else{ icur = parseInt(getStyle(obj,attr));} //获取对象obj的attr属性,parseInt()取整 var speed = (iTarget-icur)/8; speed = speed>0?Math.ceil(speed):Math.floor(speed); if(icur == iTarget){ clearInterval(obj.timer);} else { if (attr == 'opacity') { obj.style.filter = 'alpha(opacity:' + (icur + speed) + ')'; obj.style.opacity = (icur + speed) / 100; } else { obj.style[attr] = icur + speed + 'px';} } },30); } </script> </head> <body> <ul> <li id="li1"></li> </ul> </body> </html>
举报