本文據下址理得http://www.zhangxinxu.com/wordpress/?p=552javascript
css3:(有兼容限制)css
-webkit-transform:rotate(-90deg);-moz-transform:rotate(-90deg); transform:rotate(-90deg);
ie濾鏡:html
filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
「rotation=3」,這裏的參數能夠是0,1,2,3,但沒有4,5....這裏的數值*π/2=選轉的度數,也就是乘以90度。可是隻能侷限於90度的倍數,若是要突破這種侷限,能夠採用以下矩陣變換的方式獲得旋轉60度,ie8下要使用-ms-filter代替filterjava
filter:progid:DXImageTransform.Microsoft.Matrix(M11=0.5,M12=-0.866,M21=0.866,M22=0.5,SizingMethod='auto expand');
也就是以下公式,這裏的rotation表示角度,如60,css3
filter:progid:DXImageTransform.Microsoft.Matrix(M11=cos(roation),M12=-sin(roation),M21=sin(roation),M22=cos(roation),SizingMethod='auto expand');
對比web
css3旋轉不會改變佔位大小可能覆蓋別的元素,ie濾鏡會改變擠開別的元素canvas
不考慮opera,<firfox2,就很容易實現旋轉,不然就使用canvas實現:api
canvas是html元素,沒有行爲,但提供api給js繪圖到一個畫布;瀏覽器
ie下實現:ide
<!- -[if IE]><script type="text/javascript" src="http://www.zhangxinxu.com/style/js/excanvas.js"></script><![endif]- ->(這裏爲了防止註釋衝突,雙連線用空格隔開了,而後做者也推廣了本身)
其中的連接是使ie大部分支持canvas的js插件
jQuery的旋轉插件(ie濾鏡+其餘瀏覽器使用canvas)(見附錄)
使用方法:$("選擇器").rotateLeft()
; 與 $("選擇器").rotateRight()
;但若是不作任何特別的處理的話,此插件只支持一次的圖片旋轉效果,由於分析插件可知,當運行一次canvas旋轉後,canvas標籤會將原來的圖片標籤代替掉,使得圖片的連續旋轉遇到麻煩,須要每次旋轉後從新輸入圖片相關的HTML代碼。
param.right.click(function(){ if(!$("img#rotImg").length){ param.box.html('<img id="rotImg" src="http://image.zhangxinxu.com/image/study/s/s512/mm1.jpg" />'); } fun.right(); //執行向右旋轉 return false; });
更強大的jq旋轉插件如 jQueryRotate.js
附錄:
jq旋轉插件:
jQuery.fn.rotate = function(angle,whence) { var p = this.get(0); // we store the angle inside the image tag for persistence if (!whence) { p.angle = ((p.angle==undefined?0:p.angle) + angle) % 360; } else { p.angle = angle; } if (p.angle >= 0) { var rotation = Math.PI * p.angle / 180; } else { var rotation = Math.PI * (360+p.angle) / 180; } var costheta = Math.round(Math.cos(rotation) * 1000) / 1000; var sintheta = Math.round(Math.sin(rotation) * 1000) / 1000; //alert(costheta+","+sintheta); if (document.all && !window.opera) { var canvas = document.createElement('img'); canvas.src = p.src; canvas.height = p.height; canvas.width = p.width; canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')"; } else { var canvas = document.createElement('canvas'); if (!p.oImage) { canvas.oImage = new Image(); canvas.oImage.src = p.src; } else { canvas.oImage = p.oImage; } canvas.style.width = canvas.width = Math.abs(costheta*canvas.oImage.width) + Math.abs(sintheta*canvas.oImage.height); canvas.style.height = canvas.height = Math.abs(costheta*canvas.oImage.height) + Math.abs(sintheta*canvas.oImage.width); var context = canvas.getContext('2d'); context.save(); if (rotation <= Math.PI/2) { context.translate(sintheta*canvas.oImage.height,0); } else if (rotation <= Math.PI) { context.translate(canvas.width,-costheta*canvas.oImage.height); } else if (rotation <= 1.5*Math.PI) { context.translate(-costheta*canvas.oImage.width,canvas.height); } else { context.translate(0,-sintheta*canvas.oImage.width); } context.rotate(rotation); context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height); context.restore(); } canvas.id = p.id; canvas.angle = p.angle; p.parentNode.replaceChild(canvas, p); } jQuery.fn.rotateRight = function(angle) { this.rotate(angle==undefined?90:angle); } jQuery.fn.rotateLeft = function(angle) { this.rotate(angle==undefined?-90:-angle); }