Javascript chained motion framework-line-by-line code analysis allows you to easily understand the principle of motion
The so-called chain movement is a link. In fact, many of our sports refer to stages. The first stage is complete and the next stage starts to change. This chained motion frame is used to solve these problems.
Let's take a look at the previous motion framework. The following is the Javascript code.
function getStyle(obj, name) { if (obj.currentStyle) { return obj.currentStyle[name]; } else { return getComputedStyle(obj, null)[name]; }}function startMove(obj, attr, iTarget) { clearInterval(obj.time); obj.time = setInterval(function() { var cur = 0; if (attr == 'opacity') { cur = Math.round(parseFloat(getStyle(obj, attr)) * 100); } else { cur = parseInt(getStyle(obj, attr)); } var speed = (iTarget - cur) / 6; speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); if (cur == iTarget) { clearInterval(obj.time); } else { if (attr == 'opacity') { obj.style.filter = 'alpha(opacity=' + cur + speed + ')'; obj.style.opacity = (cur + speed) / 100; } else { obj.style[attr] = cur + speed + 'px'; } } }, 30);}
In fact, it is equivalent to a stage-type motion frame. When an object moves somewhere, it stops. So how can we achieve Chain movement?
We are adding a fnEnd parameter. This is a function that will be called at the end of the motion.
Of course, this function can be passed without being passed, so you need to make a judgment. It can be called only when it is passed in.
The principle is: a motion is completed at the start of the next motion.
In this way, we can complete the chain movement. Let's take a look at the code.
<script src="js/move_lianshi.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> window.onload=function(){ var oDiv=document.getElementById("div1"); oDiv.onmouseover=function(){ startMove(oDiv,'width',300,function(){ startMove(oDiv,'height',300); }); }; } </script>
Javascript:
function getStyle(obj, name) { if (obj.currentStyle) { return obj.currentStyle[name]; } else { return getComputedStyle(obj, null)[name]; }}function startMove(obj, attr, iTarget,fnEnd) { clearInterval(obj.time); obj.time = setInterval(function() { var cur = 0; if (attr == 'opacity') { cur = Math.round(parseFloat(getStyle(obj, attr)) * 100); } else { cur = parseInt(getStyle(obj, attr)); } var speed = (iTarget - cur) / 6; speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); if (cur == iTarget) { clearInterval(obj.time); if(fnEnd)fnEnd(); } else { if (attr == 'opacity') { obj.style.filter = 'alpha(opacity=' + cur + speed + ')'; obj.style.opacity = (cur + speed) / 100; } else { obj.style[attr] = cur + speed + 'px'; } } }, 30);}
In this way, the Div will first become wider and higher.
We still have limitations in this motion frame. What is that?
So what should I do if I want the Div to become wider and wider at the same time? In the next update, we will solve this problem and launch a perfect motion frame that supports most movements.
Coming soon ~