我想在IE9下實現點擊按鈕進行圖片的翻轉,之前使用了濾鏡,但有一個問題,圖片在旋轉時不是以圖片中心為定點進行旋轉,在網上查了css3的一個屬性transform 可以實現以中心點進行旋轉,但試了好多,都沒實現,下面是濾鏡實現代碼:
/**
* 圖片旋轉
* @param id 圖片id
* @param angle 旋轉度數
* @param whence 旋轉方向、從何處開始旋轉
*/
rotate : function(id,angle,whence) {
var p = document.getElementById(id);
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.cos(rotation);
var sintheta = Math.sin(rotation);
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.onmousewheel = p.onmousewheel;
canvas.angle = p.angle;
p.parentNode.replaceChild(canvas, p);
},
------------------------------------------------------------
在css樣式中:
#imgDiv{
-ms-transform:rotate(90deg); /* IE 9 */
}
這樣可以實現旋轉,
但我想使用js進行動態左右旋轉,求大神們賜教!謝謝
jquery.rotate.js