Home >Web Front-end >JS Tutorial >js sliding animation effect example sharing

js sliding animation effect example sharing

小云云
小云云Original
2018-03-14 17:30:272419browse

This article mainly shares with you examples of js sliding animation effects. I hope everyone can learn to use js to create simple sliding animation effects.

<!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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn