r/JavaScriptTips • • Jun 26 '23

How to Moving an Elements in JavaScript

Moving Elements in JavaScript means automatically changing the position of an element without any interaction. We can move the element by using both CSS and Javascript.  If you want to move an HTML element in javascript, we need to create setInterval function and use the "left" property inside it. You can execute a function repeatedly at a specific time interval by creating the setInterval function. So, in this article, we will see how to move elements in Javascript.

JS:

 	var x =0
	window.onload = function(){
		var box = document.getElementById("box")
		setInterval(function(){
			x++
			box.style.left=x + 'p'
		},10)
	}

Click here to see : 3 Steps to moving element

0 Upvotes

0 comments sorted by