编写了一个函数,用来做链式运动时改变属性值,但是进入链式运动后没有再执行第二次调用的函数,请问为什么,附上源码html:<html><head><meta charset="UTF-8"><title>旋转图标</title><link rel="stylesheet" type="text/css" href="../css/RotateSport.css" /><script src="../js/RotateSport.js"></script></head><body><div id="box"><img src="../img/7.jpg" /></div></body></html>js:window.onload=function(){var oDiv=document.getElementById("box");var oImg=oDiv.getElementsByTagName("img")[0];oDiv.onmouseover=function(){startMove(oImg,{top:-170,opacity:30},function(){oImg.style.top=30+"px";startMove(oImg,{opacity:100,top:25});});}}function startMove(obj,json,fnEnd){var timer=null;var alpha=100;obj.timer=setInterval(function(){clearInterval(obj.timer);for(attr in json) {var key = true;//获取当前值var icur;if ("opacity" == attr) {icur = Math.round(parseFloat(getstyle(obj, attr)) * 100);}else {icur = parseInt(getstyle(obj, attr));}//改变速度var speed = (json[attr] - icur) /10;speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);//检测当前值if (json[attr] !== icur) {key = false;}if ("opacity" == attr) {obj.style.filter = "alpha(opacity:" + (speed + icur) + ")";obj.style.opacity = (speed + icur) / 100;}else {obj.style[attr] =(speed + icur)+ "px";}if(key){clearInterval(obj.timer);if(typeof fnEnd=="function"){fnEnd();}}}},30);}function getstyle(obj,attr){return getComputedStyle?getComputedStyle(obj,false)[attr]:obj.currentStyle[attr];}
1 回答
白衣非少年
TA贡献1155条经验 获得超0个赞
<!DOCTYPE HTML><html> <head> <meta charset="UTF-8"> <title>旋转图标</title><style>#box>img{top:30px;position:absolute;width:100px;height:100px;}</style> <script> window.onload = function() { var oDiv = document.getElementById("box"); var oImg = oDiv.getElementsByTagName("img")[0]; oDiv.onmouseover = function() { startMove(oImg, { top: -170, opacity: 30 }, function() { oImg.style.top = 30 + "px"; startMove(oImg, { opacity: 100, top: 25 }); }); } } function startMove(obj, json, fnEnd) { //在最上面清除即可。。。clearInterval(obj.timer); var timer = null; var alpha = 100; obj.timer = setInterval(function() { for (attr in json) { var key = true; //获取当前值 var icur; if ("opacity" == attr) { icur = Math.round(parseFloat(getstyle(obj, attr)) * 100); } else { icur = parseInt(getstyle(obj, attr)); } //改变速度 var speed = (json[attr] - icur) / 10; speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); //检测当前值 if (json[attr] !== icur) { key = false; } if ("opacity" == attr) { obj.style.filter = "alpha(opacity:" + (speed + icur) + ")"; obj.style.opacity = (speed + icur) / 100; } else { obj.style[attr] = (speed + icur) + "px"; } if (key) { clearInterval(obj.timer); if (typeof fnEnd == "function") { fnEnd(); } } } }, 30); } function getstyle(obj, attr) { return getComputedStyle ? getComputedStyle(obj, false)[attr] : obj.currentStyle[attr]; } </script></head> <body> <div id="box"> <img src="../img/7.jpg" /> </div></body> </html>
添加回答
举报
0/150
提交
取消