开发者

CSS Transitions Rotating img

开发者 https://www.devze.com 2023-04-13 00:53 出处:网络
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 jQuerybut i need it by css 开发者_如

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 .


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" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号