Home >Web Front-end >JS Tutorial >js sliding animation effect example sharing
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>JS animation</title> <style> *{ margin: 0; padding: 0; } p{ background-color: green; width: 100px; height: 100px; } </style></head><body><p id="p1"></p><script> p1.style.position = 'absolute' p1.style.left = 0 var n = 0 var id = setInterval( ()=> { n = n + 5 p1.style.left = n + 'px' if(n>100){ window.clearInterval(id) } },1000/24)</script></body></html>
We know that due to visual retention, the combination of static images gives us the illusion of animation. Here I move this small square 24 times per second, 5px each time, using this illusion to make it look like it is moving slowly.
First set the style of p1 so that we can control its offset through left. The offset is controlled by n, which is always increasing.
Since the animation needs to be stopped, we use if judgment to clear the interval named id when n>100, so that it no longer works.
Related recommendations:
jQuery method to implement menu-sensitive mouse sliding animation effect_jquery
The above is the detailed content of js sliding animation effect example sharing. For more information, please follow other related articles on the PHP Chinese website!