How can I make the small square box move to the left | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can I make the small square box move to the left

My js animation is not working. After the small box moves to the right and down, it refuses to move to the left. What is wrong? https://code.sololearn.com/W5wObr7tsIh1/?ref=app

23rd Mar 2020, 3:26 PM
Fasasi Sherif
Fasasi Sherif - avatar
7 Answers
+ 3
This also works: var pos3 = 150; function move3() { if ( pos3 <= 0){ clearInterval(t); } else { pos3 -= 1; inside[0].style.left = pos3+"px"; } } https://code.sololearn.com/WdEC7l5bq0b7/?ref=app
23rd Mar 2020, 4:56 PM
BroFar
BroFar - avatar
+ 2
Thanks very much . But why does style.right in move3() not work.
23rd Mar 2020, 3:50 PM
Fasasi Sherif
Fasasi Sherif - avatar
+ 2
Thanks @BroFar
23rd Mar 2020, 6:29 PM
Fasasi Sherif
Fasasi Sherif - avatar
+ 2
Thanks @Ja Play
23rd Mar 2020, 6:29 PM
Fasasi Sherif
Fasasi Sherif - avatar
+ 2
23rd Mar 2020, 6:56 PM
Fasasi Sherif
Fasasi Sherif - avatar
+ 1
Good try. Because you are fixing the position with style.left in move1(), don't try to fix position with style.right in move3() Instead, you should still change style.left with pos1 -= 1 in move3() A fix for you: https://code.sololearn.com/Wg9on8H9HnxQ/?ref=app Try same concept for moving up along the left edge in a move4()
23rd Mar 2020, 3:33 PM
Gordon
Gordon - avatar
+ 1
The reference point of canvas (x,y) is its left upper corner (0,0). Therefore, the distances between the left and the upper side of a shape was defined (determined) for its position. Thus style.right do not work.
23rd Mar 2020, 5:55 PM
JaScript
JaScript - avatar