You can't achieve this using the for
loop and setting new position of the div
here. This is due to the fact, that JavaScript is single-threaded. I suggest you to read the answer to this question, it describes the problem quite well.
To somehow 'unblock' the thread you can use setTimeout
calls and set new position in their callbacks. See the fiddle I've created for you to show you this method in action. This is a kind of a workaround of JS limitations but it works. As described in the answer I linked in the previous paragraph:
The idea behind it is that you give the UI some time to update every once in awhile. Since there's no synchronous sleep function in Javascript, the only way to achieve this is to use setTimeout (or setInterval with a little bit more complicate logic) to delay the execution of every loop of your complex calculations. This would give the browser some time to update the UI between loops, giving the visual effect of multiple things happening simultaneously. A few ms should be more than enough for the UI to reflect the latest changes.
However, the best way to do animations on web pages is by using CSS transitions. Just in case you are not limited to doing it in JS, I've created a fiddle to show you this solution as well (see how smooth the animation is).
Further reading: