Aaditi Sharma
Aaditi Sharma

Reputation: 766

Rotate the div at an angle using javascript

I want to rotate the div by an angle, say 60 degrees, clockwise using javascript.

Across a lot of links via google, a logical solution seems using the Math library, but I don't know how to implement the above problem. Trigonometry was never my cup of tea :|

It can be done easily via jQuery, but I'm not allowed to use any of the libraries. Upon googling, i had come across sandpaper,etc. but have to do it with available javascript functions. Also, since it's on IE6, CSS3 cannot be used. :/

So, say I want to rotate the test div by an angle, how do i go about it?

<html>
    <head>
        <style>#test {background:#000; width:100px; height:100px;}</style>
    </head>

<body>
    <div id="test"></div>
</body>
</html>

Thanks :)

Upvotes: 0

Views: 1301

Answers (1)

harpo
harpo

Reputation: 43168

Okay, try this. This spins a box (in IE8) and should work in IE6.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
    <head>
        <title>IE6 rotation test</title>
        <style type="text/css">
            #spinner {
                position: absolute;
                top: 100px;
                left: 100px;
                width: 200px;
                height: 100px;
                border: 2px solid red;
                filter:progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand')
            }
        </style>
        <script type="text/javascript">
var spinnerAngle = 0;

window.onload = function() {
    setInterval(function () {
        spinnerAngle += Math.PI / 64;
        setElementAngle(document.getElementById('spinner'), spinnerAngle);
    }, 25);
}

function setElementAngle(ele, radians)
{
     costheta = Math.cos(radians);
     sintheta = Math.sin(radians);

     ele.filters.item(0).M11 = costheta;
     ele.filters.item(0).M12 = -sintheta;
     ele.filters.item(0).M21 = sintheta;
     ele.filters.item(0).M22 = costheta;
}
        </script>
    </head>
    <body>
        <div id="spinner"></div>
    </body>
</html>

See the documentation for the Matrix filter. The "sizingMethod" setting is apparently necessary.

Upvotes: 2

Related Questions