How can I create a moving animation element using JavaScript | SoloLearn: Learn to code for FREE!

+2

How can I create a moving animation element using JavaScript

5/18/2020 2:05:40 AM

Elvis Nestory

2 Answers

New Answer

+1

This code is from sololearn <!DOCTYPE html> <html> <head> <title>Page Title</title> <style> #container { width: 200px; height: 200px; background: green; position: relative; } #box { width: 50px; height: 50px; background: red; position: absolute; } </style> <script > window.onload = function() { var pos = 0; //our box element var box = document.getElementById('box'); var t = setInterval(move, 10); function move() { if(pos >= 150) { clearInterval(t); } else { pos += 1; box.style.left = pos+'px'; } } }; </script> </head> <body> <div id="container"> <div id="box"> </div> </div> </body> </html> This will create a moving animation. Or just visit 👇👇👇👇 https://www.sololearn.com/learn/JavaScript/2756/ Hope this will help

+1

CSS animations might help here too.