JS实现图片旋转的三种方法及兼容性处理

作者:闭包旅人

时间:2026-10-28

浏览:439

本文详细解析JavaScript实现图片旋转的三种主流方案:jQueryRotate.js、Microsoft Matrix对象及BasicImage对象。通过对比Chrome与IE8下的DOM对象变化及兼容性表现,重点阐述如何在不同浏览器中实现兼容处理,并提供针对IE8下对象替换导致后续操作复杂问题的解决方案。

文章目录

使用jQueryRotate.js实现图片旋转

方法一:借助jQueryRotate.js库



代码示例如下:


具体实现代码:

 
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1 {
width: 800px;
height: 600px;
background-color: #ff0;
position: absolute;
} 
.imgRotate {
width: 100px;
height: 80px;
position: absolute;
top: 50%;
left: 50%;
margin: -40px 0 0 -50px;
} 
</style>
</head>
<body>
<div id="div1">
<img id="img1" class="imgRotate" src="http://www.baidu.com/img/logo-yy.gif" />
<input id="input2" type="button" value="btn2"></input>
</div>
</body>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jQueryRotate.js"></script>
<script type="text/javascript">
var num = 0;
$("#input2").click(function(){
num ++;
$("#img1").rotate(90*num);
}); 
</script>
</html>

在Chrome浏览器中运行正常,旋转后图像元素类型保持为img;在IE8中运行正常,但旋转后DOM节点转换为特定结构,若继续按原ID获取元素会引发JS错误,建议通过class属性定位。此方法适用于无需后续复杂操作的场景,若涉及缩放等交互,实现难度较大。


转换后的HTML结构如下:

 
<span ...>
<rvml:group class="rvml"...>
<rvml:image class="rvml".../>
</rvml:group>
</span>

使用Microsoft提供的Matrix对象实现旋转

方法二:利用Microsoft Matrix对象



代码示例如下:


具体实现代码:

 
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1 {
width: 800px;
height: 600px;
background-color: #ff0;
position: absolute;
} 
.imgRotate {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
} 
#imgRotate {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
} 
</style>
</head>
<body>
<div id="div1">
<img id="img1" class="imgRotate" src="http://www.baidu.com/img/logo-yy.gif" />
<input id="input1" type="button" value="btn1"></input>
</div>
</body>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function rotate(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.angle = p.angle;
p.parentNode.replaceChild(canvas, p);
} 
function rotateRight(id,angle) {
rotate(id,angle==undefined?90:angle);
} 
function rotateLeft(id,angle) {
rotate(id,angle==undefined?-90:-angle);
} 
$("#input1").click(function(){
$("img.imgRotate").attr("id","imgRotate");
rotateLeft("imgRotate",90);
$("#imgRotate").attr("top","50%");
$("#imgRotate").attr("left","50%");
$("#imgRotate").attr("margin","-50px 0 0 -50px");
}); 
</script>
</html>

在Chrome浏览器中运行正常,但旋转后图像元素转换为canvas类型;在IE8中运行正常,图像元素保持为img类型。由于Matrix参数较多,使用涉及较多数学计算。



使用Microsoft提供的BasicImage对象实现旋转

方法三:利用Microsoft BasicImage对象



代码示例如下:


具体实现代码:

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<img id="image" src="http://www.baidu.com/img/logo-yy.gif" />
<input id="input2" type="button" value="btn2"></input>
</body>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
var num = 0;
$("#input2").click(function(){
num = (num + 1) % 4;
document.getElementById('image').style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+num+')';
}); 
</script>
</html>

在Chrome浏览器中无法实现旋转;在IE8中运行正常,图像元素保持为img类型。BasicImage仅支持一个参数。



对比上述三种方案,核心逻辑一致:Chrome环境下采用canvas技术,IE8环境下则采用VML或Matrix()/BasicImage()技术。在近期组件改造中,因jQueryRotate.js在IE8下生成新对象导致缩放前选中逻辑复杂,最终采取分环境处理策略:Chrome使用jQueryRotate,IE8使用BasicImage(),从而兼顾代码简洁性与可读性。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章