Javascript – Div moving in cycle rotation using JavaScript


Is it possible to rotate a Div in cyclic rotation using JavaScript. I have four DIVs in an HTML page. I need to rotate those DIVs in a cyclic rotation.

Best Solution

It's actually not hard:

function moveDiv(t,mdiv) {
            t = t + 0.05; // "time"
            var r = 10, //radius of circle
                xcenter = 400, //x location of circles centre on screen
                ycenter = 400, //y location of circles centre on screen
                x = Math.floor(xcenter + (r * Math.cos(t))), //circles parametric function
                y = Math.floor(ycenter + (r * Math.sin(t))); //circles parametric function
   = x + "px"; //set divs new coordinates
   = y + "px"; //set divs new coordinates

            setTimeout(function() { //make sure the animation keeps going
            }, 100);
    myDiv = //get div element
    moveDiv(1,myDiv); //start the animation

Haven't tested, but that's about how it should work. Make sure you set css "position" property of those divs to absolute or fixed. Also take a look at parametric equation for circle.