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>