求教js实现图片的旋转 一张图片,可以旋转,拖动鼠标顺时针或者逆时针旋转任意角度,如何做,做好友demo 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 IE上并没有解决方案只能90度90度的转其他浏览器上可以用CSS3转 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE><META NAME="Generator" CONTENT="EditPlus"><META NAME="Author" CONTENT=""><META NAME="Keywords" CONTENT=""><META NAME="Description" CONTENT=""><link href="style/reset.css" rel="stylesheet" type="text/css" /><link href="style/global.css" rel="stylesheet" type="text/css" /><style type="text/css"><!--.rotation {background:transparent url(game_x5.png) no-repeat 0 0;width:90px;height:22px;border-width:0;cursor:pointer;display:block;float:right;}img{padding:2px;border:1px solid #ccc;}--></style><script type="text/javascript"><!-- var isIE = /*@cc_on!@*/!1; function rotationPicture(img) { if (!img) return; this._img = typeof img == 'string'?document.getElementById(img):img; this.r = 1; this.addBtn(); this.bind(); } rotationPicture.prototype.addBtn = function() { this._rRight = document.createElement('BUTTON'); this._rRight.setAttribute('id','rRight'); this._rRight.className = 'rotation'; if (!isIE){ var canvas = document.createElement('CANVAS'); var ctx = canvas.getContext('2d'); canvas.setAttribute('width',this._img.width); canvas.setAttribute('height',this._img.height); ctx.drawImage(this._img,0,0); this._ghost = this._img; this._img.parentNode.replaceChild(canvas,this._img); this._img = canvas; } this._img.parentNode.insertBefore(this._rRight,this._img); }; rotationPicture.prototype.bind = function(){ var _this = this; if (window.attachEvent){ this._rRight.attachEvent('onclick',rotation); }else{ this._rRight.addEventListener('click',rotation,false); } function rotation(){ if (_this.r > 3) _this.r = 0; if(isIE){ _this._img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(Rotation=' + _this.r + ')'; }else{ var ctx = _this._img.getContext('2d'); switch (_this.r){ case 0: _this._img.setAttribute('width',_this._ghost.width); _this._img.setAttribute('height',_this._ghost.height); ctx.drawImage(_this._ghost,0,0); break; case 1: _this._img.setAttribute('width',_this._ghost.height); _this._img.setAttribute('height',_this._ghost.width); ctx.rotate(90*Math.PI/180); ctx.drawImage(_this._ghost,0,-_this._ghost.height); break; case 2: _this._img.setAttribute('width',_this._ghost.width); _this._img.setAttribute('height',_this._ghost.height); ctx.rotate(180*Math.PI/180); ctx.drawImage(_this._ghost,-_this._ghost.width,-_this._ghost.height); break; case 3: _this._img.setAttribute('width',_this._ghost.height); _this._img.setAttribute('height',_this._ghost.width); ctx.rotate(270*Math.PI/180); ctx.drawImage(_this._ghost,-_this._ghost.width,0); break; } } _this.r++; } }; //打开调用函数 window.onload = function(){ var ir = new rotationPicture('pic_xz'); };--></script></HEAD><BODY><div id="container"> <div class="head"> <h1>CSS+JS实现图片旋转</h1> <h2>(<a href="http://www.chinesefeng.com">前端技术</a>)-<strong><a href="http://www.zfsoft.com">zfsoftUED</a></strong></h2> </div> <div class="info"> <h3><span>相关说明</span></h3> <ul> <li><strong>文档类型:</strong>XHTML 1.0 Transitional</li> <li><strong>测试通过:</strong>IE6+,FF3+,Chrome</li> <li><strong>应用范围:</strong>方便用户查看一些扫描进数据库,但是显示不正常的图片.</li> </ul> </div> <div class="content"> <img src="9.jpg" id="pic_xz" alt="顺时针旋转图片" /> </div></div></BODY></HTML> #6楼IE下DXImageTransform.Microsoft.BasicImage只能90度90度的转其他浏览器下,没必要用Canvas 建议lz,其他浏览器下用CSS3,IE下用google提供的js,动态模拟canvas。要么就全用Canvas,要么全Flash 想兼容IE的话 楼上是好办法 如果楼主FLASH好的话 用FLASH是最省事 IE + Webkit 其余可以看6楼的canvas实现<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title> ImageRotation </title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">#demo { cursor:pointer; position:absolute;filter:progid:DXImageTransform.Microsoft.Matrix(M11=1.0, sizingmethod="auto expand");}</style></head><body><div id="container" style="width:500px;height:400px;position:relative;margin:0 auto"> <div id="demo"> <img src="http://images.cnblogs.com/cnblogs_com/bluedream2009/201609/o_mm.jpg" width="500" height="333" /> </div></div><script>var canTransition = function() { var el = document.createElement('foo'); el.style.cssText = '-webkit-transition: all .5s linear;'; return !!el.style.webkitTransitionProperty;}();var o = document.getElementById('demo');function rotationImg() { if (document.all) { var i = 0, j = 0, vx = 0, vy = 0, exp = 1/36; function run(angle) { j++; vx = Math.cos(angle * Math.PI / 180) * j * exp; vy = Math.sin(angle * Math.PI / 180) * j * exp; with(o.filters.item(0)) { M11 = M22 = vx; M12 = -(M21 = vy); M22 = vx; } o.style.top = (333 - o.offsetHeight) / 2 + 'px'; o.style.left = (500 - o.offsetWidth) / 2 + 'px'; } function doRotation() { o.onfilterchange = doRotation; i += 10; if (i > 359) { i = 0; o.onfilterchange = null; } run(i); } doRotation(); } else { if (canTransition) { o.style.webkitTransition = '-webkit-transform 1s ease-in'; o.style.webkitTransform='rotate(360deg)'; } } }window.onload = function() { rotationImg(); o.onclick = function() { rotationImg(); }}</script></body></html> 原来微软还有DXImageTransform.Microsoft.Matrix矩阵,受教了 参考楼上的实现,单单旋转的话,可以不用Canvas,实现全浏览器兼容(opera还没试过)<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title> ImageRotation </title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><style type="text/css">#demo { cursor:pointer; position:absolute;}</style><script type="text/javascript" src="base.src.js"></script><script type="text/javascript"> window.onload = function() { rotate(document.getElementById('demo'), 10); };</script></head><body><div id="container" style="width:500px;height:400px;position:relative;margin:0 auto"> <div id="demo"> <img src="http://images.cnblogs.com/cnblogs_com/bluedream2009/201609/o_mm.jpg" width="500" height="333" /> </div></div></body></html>base.src.jsvar userAgent = navigator.userAgent, isIE = /msie/i.test(userAgent) && !window.opera, isWebKit = /webkit/i.test(userAgent), isFirefox = /firefox/i.test(userAgent);function rotate(target, degree) { if (isWebKit) { target.style.webkitTransform = "rotate(" + degree + "deg)"; } else if (isFirefox) { target.style.MozTransform = "rotate(" + degree + "deg)"; } else if (isIE) { //chessDiv.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(rotation=" + degree + ")"; degree = degree / 180 * Math.PI; var sinDeg = Math.sin(degree); var cosDeg = Math.cos(degree); target.style.filter = "progid:DXImageTransform.Microsoft.Matrix(" + "M11=" + cosDeg + ",M12=" + (-sinDeg) + ",M21=" + sinDeg + ",M22=" + cosDeg + ",SizingMethod='auto expand')"; } else { target.style.transform = "rotate(" + degree + "deg)"; }} 谢谢,各位,我还想问一下,如何计算偏转角度呢,我用Math函数计算,取当前图片的坐标,然后,取到mousedown时候鼠标的坐标,取到一个角度,然后再mouseup的时候在取一个角度,相减就是偏转角度,,是根据顺时针和逆时针,以及偏转角度是大于90或者180度分别计算的,但是怎么总是有错误呢,是我计算的方式不对,还是考虑的不对?麻烦在请教一下? 太谢谢了,呵呵,找了半天,rainsilence高人啊 新浪微博的表情框效果怎么做的?求高手。 关于frameset的问题 谷歌浏览器的JavaScript使用问题 有关层的自动变换? 两 个框架的信息互传的问题。。在线等 关于网络连接的问题,各位高人就命呀,我这厢有礼了 请教Javascript高手!!!!急!!! jscript的自定义对象的方法中访问对象本身的方法有几种? Extjs4中itemdblclick与cellclick事件的问题 JS子窗口调用父窗口元素问题 Google地图API使用次数限制问题 JS在指定位置添加内容
其他浏览器上可以用CSS3转
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<link href="style/reset.css" rel="stylesheet" type="text/css" />
<link href="style/global.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.rotation {background:transparent url(game_x5.png) no-repeat 0 0;width:90px;height:22px;border-width:0;cursor:pointer;display:block;float:right;}
img{padding:2px;border:1px solid #ccc;}
-->
</style>
<script type="text/javascript">
<!--
var isIE = /*@cc_on!@*/!1;
function rotationPicture(img) {
if (!img) return;
this._img = typeof img == 'string'?document.getElementById(img):img;
this.r = 1;
this.addBtn();
this.bind();
}
rotationPicture.prototype.addBtn = function() {
this._rRight = document.createElement('BUTTON');
this._rRight.setAttribute('id','rRight');
this._rRight.className = 'rotation';
if (!isIE){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
canvas.setAttribute('width',this._img.width);
canvas.setAttribute('height',this._img.height);
ctx.drawImage(this._img,0,0);
this._ghost = this._img;
this._img.parentNode.replaceChild(canvas,this._img);
this._img = canvas;
}
this._img.parentNode.insertBefore(this._rRight,this._img);
};
rotationPicture.prototype.bind = function(){
var _this = this;
if (window.attachEvent){
this._rRight.attachEvent('onclick',rotation);
}else{
this._rRight.addEventListener('click',rotation,false);
} function rotation(){
if (_this.r > 3) _this.r = 0;
if(isIE){
_this._img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(Rotation=' + _this.r + ')';
}else{
var ctx = _this._img.getContext('2d');
switch (_this.r){
case 0:
_this._img.setAttribute('width',_this._ghost.width);
_this._img.setAttribute('height',_this._ghost.height);
ctx.drawImage(_this._ghost,0,0);
break;
case 1:
_this._img.setAttribute('width',_this._ghost.height);
_this._img.setAttribute('height',_this._ghost.width);
ctx.rotate(90*Math.PI/180);
ctx.drawImage(_this._ghost,0,-_this._ghost.height);
break;
case 2:
_this._img.setAttribute('width',_this._ghost.width);
_this._img.setAttribute('height',_this._ghost.height);
ctx.rotate(180*Math.PI/180);
ctx.drawImage(_this._ghost,-_this._ghost.width,-_this._ghost.height);
break;
case 3:
_this._img.setAttribute('width',_this._ghost.height);
_this._img.setAttribute('height',_this._ghost.width);
ctx.rotate(270*Math.PI/180);
ctx.drawImage(_this._ghost,-_this._ghost.width,0);
break;
}
}
_this.r++;
}
}; //打开调用函数
window.onload = function(){
var ir = new rotationPicture('pic_xz');
};
--></script>
</HEAD><BODY>
<div id="container">
<div class="head">
<h1>CSS+JS实现图片旋转</h1>
<h2>(<a href="http://www.chinesefeng.com">前端技术</a>)-<strong><a href="http://www.zfsoft.com">zfsoftUED</a></strong></h2>
</div> <div class="info">
<h3><span>相关说明</span></h3>
<ul>
<li><strong>文档类型:</strong>XHTML 1.0 Transitional</li>
<li><strong>测试通过:</strong>IE6+,FF3+,Chrome</li>
<li><strong>应用范围:</strong>方便用户查看一些扫描进数据库,但是显示不正常的图片.</li> </ul>
</div>
<div class="content">
<img src="9.jpg" id="pic_xz" alt="顺时针旋转图片" />
</div>
</div>
</BODY>
</HTML>
IE下
DXImageTransform.Microsoft.BasicImage
只能90度90度的转其他浏览器下,没必要用Canvas
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> ImageRotation </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#demo { cursor:pointer; position:absolute;filter:progid:DXImageTransform.Microsoft.Matrix(M11=1.0, sizingmethod="auto expand");}
</style>
</head>
<body>
<div id="container" style="width:500px;height:400px;position:relative;margin:0 auto">
<div id="demo">
<img src="http://images.cnblogs.com/cnblogs_com/bluedream2009/201609/o_mm.jpg" width="500" height="333" />
</div>
</div>
<script>
var canTransition = function() {
var el = document.createElement('foo');
el.style.cssText = '-webkit-transition: all .5s linear;';
return !!el.style.webkitTransitionProperty;
}();
var o = document.getElementById('demo');
function rotationImg() {
if (document.all) {
var i = 0, j = 0, vx = 0, vy = 0, exp = 1/36;
function run(angle) {
j++;
vx = Math.cos(angle * Math.PI / 180) * j * exp;
vy = Math.sin(angle * Math.PI / 180) * j * exp;
with(o.filters.item(0)) {
M11 = M22 = vx; M12 = -(M21 = vy); M22 = vx;
}
o.style.top = (333 - o.offsetHeight) / 2 + 'px';
o.style.left = (500 - o.offsetWidth) / 2 + 'px';
}
function doRotation() {
o.onfilterchange = doRotation;
i += 10;
if (i > 359) {
i = 0;
o.onfilterchange = null;
}
run(i);
}
doRotation();
} else {
if (canTransition) {
o.style.webkitTransition = '-webkit-transform 1s ease-in';
o.style.webkitTransform='rotate(360deg)';
}
}
}
window.onload = function() {
rotationImg();
o.onclick = function() {
rotationImg();
}
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> ImageRotation </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<style type="text/css">
#demo { cursor:pointer; position:absolute;}
</style>
<script type="text/javascript" src="base.src.js"></script>
<script type="text/javascript">
window.onload = function() {
rotate(document.getElementById('demo'), 10);
};
</script>
</head>
<body>
<div id="container" style="width:500px;height:400px;position:relative;margin:0 auto">
<div id="demo">
<img src="http://images.cnblogs.com/cnblogs_com/bluedream2009/201609/o_mm.jpg" width="500" height="333" />
</div>
</div>
</body>
</html>base.src.jsvar
userAgent = navigator.userAgent,
isIE = /msie/i.test(userAgent) && !window.opera,
isWebKit = /webkit/i.test(userAgent),
isFirefox = /firefox/i.test(userAgent);
function rotate(target, degree) {
if (isWebKit) {
target.style.webkitTransform = "rotate(" + degree + "deg)";
} else if (isFirefox) {
target.style.MozTransform = "rotate(" + degree + "deg)";
} else if (isIE) {
//chessDiv.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(rotation=" + degree + ")";
degree = degree / 180 * Math.PI;
var sinDeg = Math.sin(degree);
var cosDeg = Math.cos(degree);
target.style.filter = "progid:DXImageTransform.Microsoft.Matrix(" +
"M11=" + cosDeg + ",M12=" + (-sinDeg) + ",M21=" + sinDeg + ",M22=" + cosDeg + ",SizingMethod='auto expand')";
} else {
target.style.transform = "rotate(" + degree + "deg)";
}
}