Making divs Move

The top property of a div's style dictates the vertical position of the div, and the left property determines the horizontal position. You can use these properties to move a div around the screen. For example, to position a div 500 pixels from the left border of the browser window, do this:

document.getElementById('myDiv').style.left = 500;

Adding an amount to the top or left attribute of a div's style will move it vertically or horizontally. If a div is 500 pixels from the left border of the window, and you add 5 to the left property, you will move the div to a position 505 pixels from the border. Unfortunately, adding numbers to the left and top properties is not straightforward, because most browsers will stick a px at the end ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.