# Javascript animation

**URL:** <https://forum.freecodecamp.org/t/javascript-animation/368717>\
**Category:** General\
**Created:** [April 5, 2020, 11:43pm UTC](https://forum.freecodecamp.org/t/javascript-animation/368717 "2020-04-05T23:43:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azer](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@Azer](https://forum.freecodecamp.org/u/Azer)\
**Post date:** [April 5, 2020, 11:43pm UTC](https://forum.freecodecamp.org/t/javascript-animation/368717/1 "2020-04-05T23:43:23Z")

</div>

Hello, i’m trying to learn some js animations. I copied some source code from w3schools that makes a rectangle (the animate div) travel diagonally right across a larger rectangle (container div). Here is the original code:

```javascript
<!DOCTYPE html>
<html>
<style>
#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
#animate {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: red;
}
</style>
<body>

<p>
<button onclick="myMove()">Click Me</button>
</p> 

<div id ="container">
<div id ="animate"></div>
</div>

<script>
function myMove() {
  var elem = document.getElementById("animate");   
  var pos = 0;
  var id = setInterval(frame, 5);
  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++; 
      elem.style.top = pos + 'px'; 
      elem.style.left = pos + 'px'; 
    }
  }
}
</script>

</body>
</html>

```

But the problem is, i want to make a second block going in the opposite corner and make them crossover each other, but i cant get the other block to move for some reason. I’m very new to js, so apologies if it’s obvious.

This is what i tried:

```javascript
<!DOCTYPE html>
<html>
<style>
#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
#animate {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: red;
}
#animate2 {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: blue;
}
</style>
<body>

<p>
<button onclick="myMove(); myMove2();">Click Me</button>
</p> 

<div id ="container">
<div id ="animate"></div>
<div id ="animate2"></div>
</div>

<script>
function myMove() {
  var elem = document.getElementById("animate");   
  var pos = 0;
  var id = setInterval(frame, 5);
  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++; 
      elem.style.top = pos + 'px'; 
      elem.style.left = pos + 'px'; 
    }
  }
}
function myMove2() {
  var elem = document.getElementById("animate2");   
  var pos = 0;
  var id = setInterval(frame, 5);
  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++; 
      elem.style.bottom = pos + 'px'; 
      elem.style.right = pos + 'px'; 
    }
  }
}
</script>

</body>
</html>

```

Edit: i got the other block to move with this code, but now the problem is both of the squares are in the top left corner in the beginning when i need them be start at opposite corners and end at opposites

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [April 6, 2020, 12:32am UTC](https://forum.freecodecamp.org/t/javascript-animation/368717/2 "2020-04-06T00:32:21Z")

</div>

We need to see your code to know what you have tried. And a link to the code you are talking about would be nice as well. Is it [this one](https://www.w3schools.com/js/js_htmldom_animate.asp)?

If it is that one, then you can pretty much just duplicate most of the code and add another element that just does the opposite.

BTW, the JavaScript on w3schools is often fairly poor and “old school” (var everywhere and globals, etc.). You can still learn from it, it’s just not great code.

---

<div class="post-metadata">

**Author:** ![Azer](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@Azer](https://forum.freecodecamp.org/u/Azer)\
**Post date:** [April 6, 2020, 8:02am UTC](https://forum.freecodecamp.org/t/javascript-animation/368717/3 "2020-04-06T08:02:38Z")

</div>

Hello! sorry i tried putting my code yesterday but it didn’t show up for some reason, now it’s there. And it is the same code you linked but after trying to copy the function and adding another element, the blue one (the new one) isn’t moving at all, it’s stuck in the corner and i don’t know what is causing that. And thank you for trying to help 🙂

Edit: i got the other block to move with this code, but now the problem is both of the squares are in the top left corner in the beginning when i need them be start at opposites corners and end at opposites

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [April 6, 2020, 9:44am UTC](https://forum.freecodecamp.org/t/javascript-animation/368717/4 "2020-04-06T09:44:31Z")

</div>

Once you have them working individually: `setInterval` blocks, ie it stops everything else from running when it is running, so you can only do one of your things at once.

You can write the logic to move the elements seperately, but you need to have only one `setInterval` (you then call the functions inside that).

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [April 6, 2020, 3:14pm UTC](https://forum.freecodecamp.org/t/javascript-animation/368717/5 "2020-04-06T15:14:50Z")

</div>

> [@Azer](#):
>
> i got the other block to move with this code, but now the problem is both of the squares are in the top left corner in the beginning when i need them be start at opposites corners and end at opposites

Start the other one at 350 and decrement it down to 0.

---

<div class="post-metadata">

**Author:** ![Azer](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@Azer](https://forum.freecodecamp.org/u/Azer)\
**Post date:** [April 6, 2020, 3:16pm UTC](https://forum.freecodecamp.org/t/javascript-animation/368717/6 "2020-04-06T15:16:58Z")

</div>

Thanks for the explanation, got it working 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:00pm UTC](https://forum.freecodecamp.org/t/javascript-animation/368717/7 "2021-06-01T19:00:08Z")

</div>

Closed on June 1, 2021, 7:00pm UTC


