This article mainly introduces JavaScript animation to you: detailed explanation of offset and uniform animation (including the implementation of carousel images), and shares the implementation code. Friends who are interested can refer to it. I hope it can help everyone.
We know that the three major families include: offset/scroll/client. Today, let’s talk about offset and its related uniform animation.
offset in Chinese is: offset, compensation, displacement.
There is a convenient way to get the size of elements in js, which is the offset family. The offset family includes:
offsetWidth
offsetHight
offsetLeft
offsetTop
offsetParent
are introduced separately below.
1, offsetWidth and offsetHight
are used to detect the width and height + padding + border of the box itself, excluding margin. As follows:
offsetWidth = width + padding + border;
offsetHeight = Height + padding + border;
These two properties are bound to all node elements. superior. After getting it, we can get the width and height of the element node by calling these two properties.
Examples are as follows:
2, offsetLeft and offsetTop
Return the distance to the upper-level box (with positioning ); if the parent is not positioned, the body will prevail.
offsetLeft: Starting from the father’s padding, the father’s border does not count.
Example:
When the parent box is positioned, offsetLeft == style.left (after removing px). Note that the latter only recognizes inline styles. But the difference goes beyond that, as we’ll talk about later.
3. offsetParent
Detects the parent box node with positioning in the parent box. The returned result is the object's parent (with positioning).
If the parent element of the current element has no CSS positioning (position is absolute, relative, fixed), then the return result of offsetParent is body.
If the parent element of the current element has CSS positioning (position is absolute, relative, fixed), then the return result of offsetParent is the nearest parent element.
Example:
Print result:
##offsetLeft and style.left Difference
(1) The biggest difference is: offsetLeft can return the position from the left side of the unpositioned box. If there is no positioning in the parent box, the body will prevail. style.left can only get the inline style. If not, it will return "" (meaning, return empty); (2) offsetTop returns a number, while style.top returns is a string, and also has the unit: px. For example:p.offsetLeft = 100; p.style.left = "100px";
The effect is as follows:
The code is as follows:
Achieved effect://【重要】方法的封装:每间隔30ms,将盒子向右移动10px function animate(ele, target) { //要用定时器,先清除定时器 //一个盒子只能有一个定时器,这样的话,不会和其他盒子出现定时器冲突 //我们可以把定时器本身,当成为盒子的一个属性 clearInterval(ele.timer); //我们要求盒子既能向前又能向后,那么我们的步长就得有正有负 //目标值如果大于当前值取正,目标值如果小于当前值取负 var speed = target > ele.offsetLeft ? 10 : -10; //speed指的是步长 ele.timer = setInterval(function () { //在执行之前就获取当前值和目标值之差 var val = target - ele.offsetLeft; //移动的过程中,如果目标值和当前值之差如果小于步长,那么就不能在前进了 //因为步长有正有负,所有转换成绝对值来比较 if (Math.abs(val) < Math.abs(speed)) { //如果val小于步长,则直接到达目的地;否则,每次移动一个步长 ele.style.left = target + "px"; clearInterval(ele.timer); } else { ele.style.left = ele.offsetLeft + speed + "px"; } }, 30) }
is as follows: (the comments are already more detailed)
Detailed explanation of the difference between style.width and offsetWidth in js
Detailed explanation of the results of the offset character bypass vulnerability in php
The role and usage of market-offset in css
The above is the detailed content of Analysis of offset and uniform motion examples in JS. For more information, please follow other related articles on the PHP Chinese website!