Rotate a div using javascript Rotate a div using javascript javascript javascript

Rotate a div using javascript


To rotate a DIV we can add some CSS that, well, rotates the DIV using CSS transform rotate.

To toggle the rotation we can keep a flag, a simple variable with a boolean value that tells us what way to rotate.

var rotated = false;document.getElementById('button').onclick = function() {    var div = document.getElementById('div'),        deg = rotated ? 0 : 66;    div.style.webkitTransform = 'rotate('+deg+'deg)';     div.style.mozTransform    = 'rotate('+deg+'deg)';     div.style.msTransform     = 'rotate('+deg+'deg)';     div.style.oTransform      = 'rotate('+deg+'deg)';     div.style.transform       = 'rotate('+deg+'deg)';     rotated = !rotated;}