js实现图片旋转

本文介绍如何使用jQueryRotate.js、Matrix对象和BasicImage对象在Chrome和IE浏览器中实现图像旋转,包括代码示例、测试结果及不同方法的优缺点对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 使用jQueryRotate.js实现

示例代码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>   
  3. <head>   
  4. <title></title>   
  5. <style type="text/css">  
  6.     #div1 {  
  7.         width: 800px;  
  8.         height: 600px;  
  9.         background-color: #ff0;  
  10.         position: absolute;  
  11.     }  
  12.     .imgRotate {  
  13.         width: 100px;  
  14.         height: 80px;  
  15.         position: absolute;  
  16.         top: 50%;  
  17.         left: 50%;  
  18.         margin: -40px 0 0 -50px;  
  19.     }  
  20. </style>  
  21. </head>   
  22. <body>  
  23. <div id="div1">  
  24.     <img id="img1" class="imgRotate" src="http://www.baidu.com/img/logo-yy.gif" />  
  25.     <input id="input2" type="button" value="btn2"></input>  
  26. </div>  
  27. </body>   
  28. <script type="text/javascript" src="jquery.min.js"></script>  
  29. <script type="text/javascript" src="jQueryRotate.js"></script>  
  30. <script type="text/javascript">   
  31. var num = 0;  
  32. $("#input2").click(function(){  
  33.     num ++;  
  34.     $("#img1").rotate(90*num);  
  35. });  
  36. </script>   
  37. </html>  

测试结果:chrome下效果正常,旋转后img对象仍为img对象;ie8下效果正常,但旋转后img对象变为下面对象,由于对象变化,若旋转后仍按原来方法获取img对象,则会报js错误。欲获取image对象,可根据class获取。如果图像旋转后,不进行其它操作,则可用此方法。若进行其它操作,如放大、缩小图像,则此方法实现较复杂。
<span ...>
  <rvml:group class="rvml"...>
    <rvml:image class="rvml".../>
  </rvml:group>
</span>


2 使用Microsoft提供的Matrix对象

示例代码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>   
  3. <head>   
  4. <title></title>   
  5. <style type="text/css">  
  6.     #div1 {  
  7.         width: 800px;  
  8.         height: 600px;  
  9.         background-color: #ff0;  
  10.         position: absolute;  
  11.     }  
  12.     .imgRotate {  
  13.         width: 100px;  
  14.         height: 100px;  
  15.         position: absolute;  
  16.         top: 50%;  
  17.         left: 50%;  
  18.         margin: -50px 0 0 -50px;  
  19.     }  
  20.     #imgRotate {  
  21.         width: 100px;  
  22.         height: 100px;  
  23.         position: absolute;  
  24.         top: 50%;  
  25.         left: 50%;  
  26.         margin: -50px 0 0 -50px;  
  27.     }  
  28. </style>  
  29. </head>   
  30. <body>  
  31. <div id="div1">  
  32.     <img id="img1" class="imgRotate" src="http://www.baidu.com/img/logo-yy.gif" />  
  33.     <input id="input1" type="button" value="btn1"></input>  
  34. </div>  
  35. </body>   
  36. <script type="text/javascript" src="jquery.min.js"></script>  
  37. <script type="text/javascript">   
  38. function rotate(id,angle,whence) {  
  39.     var p = document.getElementById(id);  
  40.   
  41.     // we store the angle inside the image tag for persistence  
  42.     if (!whence) {  
  43.         p.angle = ((p.angle==undefined?0:p.angle) + angle) % 360;  
  44.     } else {  
  45.         p.angle = angle;  
  46.     }  
  47.   
  48.     if (p.angle >= 0) {  
  49.         var rotation = Math.PI * p.angle / 180;  
  50.     } else {  
  51.         var rotation = Math.PI * (360+p.angle) / 180;  
  52.     }  
  53.     var costheta = Math.cos(rotation);  
  54.     var sintheta = Math.sin(rotation);  
  55.   
  56.     if (document.all && !window.opera) {  
  57.         var canvas = document.createElement('img');  
  58.   
  59.         canvas.src = p.src;  
  60.         canvas.height = p.height;  
  61.         canvas.width = p.width;  
  62.   
  63.         canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";  
  64.     } else {  
  65.         var canvas = document.createElement('canvas');  
  66.         if (!p.oImage) {  
  67.             canvas.oImage = new Image();  
  68.             canvas.oImage.src = p.src;  
  69.         } else {  
  70.             canvas.oImage = p.oImage;  
  71.         }  
  72.   
  73.         canvas.style.width = canvas.width = Math.abs(costheta*canvas.oImage.width) + Math.abs(sintheta*canvas.oImage.height);  
  74.         canvas.style.height = canvas.height = Math.abs(costheta*canvas.oImage.height) + Math.abs(sintheta*canvas.oImage.width);  
  75.   
  76.         var context = canvas.getContext('2d');  
  77.         context.save();  
  78.         if (rotation <= Math.PI/2) {  
  79.             context.translate(sintheta*canvas.oImage.height,0);  
  80.         } else if (rotation <= Math.PI) {  
  81.             context.translate(canvas.width,-costheta*canvas.oImage.height);  
  82.         } else if (rotation <= 1.5*Math.PI) {  
  83.             context.translate(-costheta*canvas.oImage.width,canvas.height);  
  84.         } else {  
  85.             context.translate(0,-sintheta*canvas.oImage.width);  
  86.         }  
  87.         context.rotate(rotation);  
  88.         context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);  
  89.         context.restore();  
  90.     }  
  91.     canvas.id = p.id;  
  92.     canvas.angle = p.angle;  
  93.     p.parentNode.replaceChild(canvas, p);  
  94. }  
  95.   
  96. function rotateRight(id,angle) {  
  97.     rotate(id,angle==undefined?90:angle);  
  98. }  
  99.   
  100. function rotateLeft(id,angle) {  
  101.     rotate(id,angle==undefined?-90:-angle);  
  102. }  
  103. $("#input1").click(function(){  
  104.     $("img.imgRotate").attr("id","imgRotate");  
  105.     rotateLeft("imgRotate",90);  
  106.     $("#imgRotate").attr("top","50%");  
  107.     $("#imgRotate").attr("left","50%");  
  108.     $("#imgRotate").attr("margin","-50px 0 0 -50px");  
  109. });  
  110. </script>   
  111. </html>  

测试结果:chrome下效果正常,但旋转后img对象变为canvas对象;ie8下效果正常,旋转后img对象仍为img对象。Matrix()参数较多,使用时需较多计算。


3 使用Microsoft提供的BasicImage对象

示例代码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  5. </head>  
  6. <body>  
  7.     <img id="image" src="http://www.baidu.com/img/logo-yy.gif" />  
  8.     <input id="input2" type="button" value="btn2"></input>  
  9. </body>  
  10. <script type="text/javascript" src="jquery.min.js"></script>  
  11.   
  12. <script type="text/javascript">  
  13.     var num = 0;  
  14.     $("#input2").click(function(){  
  15.         num = (num + 1) % 4;  
  16.         document.getElementById('image').style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+num+')';  
  17.     });  
  18. </script>  
  19. </html>  

测试结果:chrome下不能旋转;ie8下效果正常,旋转后img对象仍为img对象。BasicImage()仅一个参数。

查看这三种方法的代码会发现,本质上是一种解决方案:chrome下使用canvas对象实现,ie8下使用VML或者Matrix()或BasicImage()实现。本人近期改造一个组件:其中涉及到旋转、放大图片,由于jQueryRotate.js在ie8下会生成一个新的对象,导致放大图片前选择图片时,需要进行特殊处理。后决定对chrome、ie8分开处理,chrome下使用jQueryRotate实现,ie8下使用BasicImage()实现,保证了代码的简洁性和可读性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值