Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. How do you pause an animation?

How do you pause an animation?

Scheduled Pinned Locked Moved JavaScript
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marc Hede
    wrote on last edited by
    #1

    I am making a live subway map, where I have divs representing trains, moving up and down the map. I had no problem making them move, but I would like them to stop for a few seconds at each station. Can someone tell me how I can include this in my code? What line do I need to add, and where?

    window.onload = () => {
    startSetTimeoutAnimation();
    };

    function startSetTimeoutAnimation() {
    const refreshRate = 1000 / 60;
    const maxXPosition = 400;
    let rect = document.getElementById('rect0');
    let speedX = 0.01;
    let positionX = 25;

    window.setInterval(() => {
      positionX = positionX + speedX;
      if (positionX > maxXPosition || positionX < 25) {
        speedX = speedX \* (-1);
      }
      rect.style.top = positionX + 'px';
    }, refreshRate);
    

    }

    S 1 Reply Last reply
    0
    • M Marc Hede

      I am making a live subway map, where I have divs representing trains, moving up and down the map. I had no problem making them move, but I would like them to stop for a few seconds at each station. Can someone tell me how I can include this in my code? What line do I need to add, and where?

      window.onload = () => {
      startSetTimeoutAnimation();
      };

      function startSetTimeoutAnimation() {
      const refreshRate = 1000 / 60;
      const maxXPosition = 400;
      let rect = document.getElementById('rect0');
      let speedX = 0.01;
      let positionX = 25;

      window.setInterval(() => {
        positionX = positionX + speedX;
        if (positionX > maxXPosition || positionX < 25) {
          speedX = speedX \* (-1);
        }
        rect.style.top = positionX + 'px';
      }, refreshRate);
      

      }

      S Offline
      S Offline
      Sherin_Mathew
      wrote on last edited by
      #2

      Try This Code:

      div {
      width: 100px;
      height: 100px;
      background: blue;
      position: relative;
      animation: mymove 3s infinite;
      animation-play-state: paused;
      }

      @keyframes mymove {
      from {left: 0px;}
      to {left: 400px;}
      }

      Click the buttons to Play/Pause the animation:

      Play
      Pause

      function myPlayFunction() {
      document.getElementById("myDIV").style.animationPlayState = "running";
      }

      function myPauseFunction() {
      document.getElementById("myDIV").style.animationPlayState = "paused";
      }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups