-2

I need to Transitions Rotating img by click to be 90 degree and after click the button again the img rotate to get to 0 degree . I have web site that have these by jQuery but i need it by css only .

user965330
  • 21
  • 3

1 Answers1

3

The transform:rotate() CSS property has to be used. I've written a (simple?) pure JS function for this purpose.

JS:

function rotate(elem, angle){
    //Cross-browser:
    var css = ["-moz-","-webkit-","-ms-","-o-","",""]
               .join("transform:rotate(" + angle + "deg);")
    elem.style.cssText += css;
}

Example, Fiddle: http://jsfiddle.net/zvuyc/:

<script>
window.onload = function(){
    var angle = 0;
    document.getElementById("button").onclick = function(){
        angle += 90 % 360;
        rotate(document.getElementById("image"), angle);
    }
}
</script>
<input type="button" id="button" value="Rotate by 90deg">
<img src="http://simplyeng.com/wp-content/uploads/2008/11/indiana_tux.png" id="image" />
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • The cssText can get pretty long this way (and invokes the css parser). This is a variation where it sets the properties instead: http://jsfiddle.net/2tZja/ – Timo Tijhof Oct 05 '12 at 03:52