I have tried to make an animation in JavaScript, but there is some mistake in it. Please help me out. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

I have tried to make an animation in JavaScript, but there is some mistake in it. Please help me out.

https://code.sololearn.com/WIXqYfOjOUHk/?ref=app This is my code ☝. I have made an animation in which there is a blue container. I have also made a red box which is situated at the top left corner of the container. I want to make an animation in which the red box moves from left to right, and back from right to left. But, I am only successful to make it move from left to right. Please tell me what's the mistake in my code and the solution. Thanks!

29th May 2018, 4:26 AM
Rahul Hemdev
Rahul Hemdev - avatar
3 Answers
+ 4
In your code, you only need the ones inside the onload function. You have done half of what you want to do. The box goes to the right by incrementing the pos variable by 1. Now, to go back, you need to increment it by -1. This can be done by using and increment variable that changes from 1 to -1 and vice versa. Check out this code to see how it happens: https://code.sololearn.com/W3n3hy9Uwng8/?ref=app
29th May 2018, 7:19 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 4
Thanks friend!👍 A last question, What does 'px' stand for?
29th May 2018, 7:24 AM
Rahul Hemdev
Rahul Hemdev - avatar
+ 2
pixel.
29th May 2018, 8:06 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar