1

I've found this example in w3schools and want to modify its animation into my liking. Basically it has two divs, one as a container and the other is for the animation. Originally the animated div can go one direction from top to bottom or top to left etc. But what I want to happen is, as soon as it animates already from top to bottom, I want it to move back from bottom to top. How to achieve this?

Here is the code:

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

<p>
<button onclick="Move()">Click</button>
</p> 

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

<script>
function Move() {
var elem = document.getElementById("animate");   
var pos = 0;
var id = setInterval(frame, 5);
function frame() {

if (pos < 300) {
  pos++;
  elem.style.top = pos + 'px'; 
  elem.style.bottom = pos + 'px'; 
}

}
}
</script>

</body>
</html>
2
  • 1
    this is a great reference of animations robertpenner.com/easing and the rest google.es/… Commented Jul 18, 2017 at 8:18
  • You have to set a "direction", otherwise if you only check vertical position it won't work. Commented Jul 18, 2017 at 8:20

4 Answers 4

1

Have you considered using keyframes instead of js?

#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
#animate {
  width: 100px;
  height: 100px;
  position: absolute;
  left: 145px;
  background-color: red;
  animation: move 3s linear infinite;
}

@keyframes move {
  0% {
    top: 0;
  }
  50% {
    top: 300px;
  }
  100% {
    top: 0;
  }
}
<body>
  <div id="container">
    <div id="animate"></div>
  </div>
</body>

Sign up to request clarification or add additional context in comments.

Comments

1

Its better to use css animations, but if you want to do in this way then you have to set a direction. I modify your code and add direction in it.

function Move() {
var elem = document.getElementById("animate");
var pos = 0;
var direction = "down";
var id = setInterval(frame, 5);
function frame() {
if (direction=="down") {
  pos++;
  elem.style.top = pos + 'px'; 
  elem.style.bottom = pos + 'px'; 
} else {
  pos--;
  elem.style.top = pos + 'px'; 
  elem.style.bottom = pos + 'px'; 
}
if (pos == 300) {
  direction = "up";
}
if (pos == 0) {
  direction = "down";
}
}
}
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 100px;
height: 100px;
position: absolute;
left: 145px;
background-color: red;
}
<p>
<button onclick="Move()">Click</button>
</p> 

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

Comments

0

you can keep a variable to check which direction the box is going and change it based on the boundary conditions. Like if you reach position 0 you can make direction down and if you reach position 300 make direction up.

function Move() {
var elem = document.getElementById("animate");   
var pos = 0;
var direction = 'down';
var id = setInterval(frame, 5);
function frame() {

if (pos >= 300) {
   direction = 'up';
} else if (pos <= 0) {
direction = 'down';
}
 if (direction === 'down'){
  pos++; 
} else if (direction === 'up'){
  pos--;
}
  elem.style.top = pos + 'px'; 
  elem.style.bottom = pos + 'px'; 


}
}
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 100px;
height: 100px;
position: absolute;
left: 145px;
background-color: red;
}
<p>
<button onclick="Move()">Click</button>
</p> 

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

Comments

0

You can modify you frame() function to check direction and then add/subtract position accordingly E.g

function frame() {

  if(pos >= 300 ) {
    dir = 1;
  }else if (pos <=0) {
    dir = 0;
  }

  if (dir ===0) {
    pos++;
  } else {
    pos--;
  }

   elem.style.top = pos + 'px'; 
   elem.style.bottom = pos + 'px'; 
}

You can find this working on this JSBin

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.