js實(shí)現(xiàn)圖片旋轉(zhuǎn)的三種方法
示例代碼:
<!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>
測(cè)試結(jié)果:chrome下效果正常,旋轉(zhuǎn)后img對(duì)象仍為img對(duì)象;ie8下效果正常,但旋轉(zhuǎn)后img對(duì)象變?yōu)橄旅鎸?duì)象,由于對(duì)象變化,若旋轉(zhuǎn)后仍按原來方法獲取img對(duì)象,則會(huì)報(bào)js錯(cuò)誤。欲獲取image對(duì)象,可根據(jù)class獲取。如果圖像旋轉(zhuǎn)后,不進(jìn)行其它操作,則可用此方法。若進(jìn)行其它操作,如放大、縮小圖像,則此方法實(shí)現(xiàn)較復(fù)雜。
<span ...>
<rvml:group class="rvml"...>
<rvml:image class="rvml".../>
</rvml:group>
</span>
2 使用Microsoft提供的Matrix對(duì)象
示例代碼:
<!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);
// 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.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>
測(cè)試結(jié)果:chrome下效果正常,但旋轉(zhuǎn)后img對(duì)象變?yōu)閏anvas對(duì)象;ie8下效果正常,旋轉(zhuǎn)后img對(duì)象仍為img對(duì)象。Matrix()參數(shù)較多,使用時(shí)需較多計(jì)算。
3 使用Microsoft提供的BasicImage對(duì)象
示例代碼:
<!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>
測(cè)試結(jié)果:chrome下不能旋轉(zhuǎn);ie8下效果正常,旋轉(zhuǎn)后img對(duì)象仍為img對(duì)象。BasicImage()僅一個(gè)參數(shù)。
查看這三種方法的代碼會(huì)發(fā)現(xiàn),本質(zhì)上是一種解決方案:chrome下使用canvas對(duì)象實(shí)現(xiàn),ie8下使用VML或者M(jìn)atrix()或BasicImage()實(shí)現(xiàn)。本人近期改造一個(gè)組件:其中涉及到旋轉(zhuǎn)、放大圖片,由于jQueryRotate.js在ie8下會(huì)生成一個(gè)新的對(duì)象,導(dǎo)致放大圖片前選擇圖片時(shí),需要進(jìn)行特殊處理。后決定對(duì)chrome、ie8分開處理,chrome下使用jQueryRotate實(shí)現(xiàn),ie8下使用BasicImage()實(shí)現(xiàn),保證了代碼的簡(jiǎn)潔性和可讀性。
相關(guān)文章
用Javascript同時(shí)提交多個(gè)Web表單的方法
使用Javascript同時(shí)提交多個(gè)Web表單的方法2009-12-12javascript實(shí)現(xiàn)的仿51job地址多項(xiàng)選擇方式效果
分享一個(gè)類似51job方式的地址選擇效果2009-12-12JavaScript原始數(shù)據(jù)類型Symbol的用法詳解
Symbol是ES6中引入的一種新的基本數(shù)據(jù)類型,用于表示一個(gè)獨(dú)一無二的值。它是JavaScript中的第七種數(shù)據(jù)類型。本文將詳細(xì)講講Symbol的使用,需要的可以參考一下2022-11-11JavaScript實(shí)現(xiàn)的多種鼠標(biāo)拖放效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的多種鼠標(biāo)拖放效果,涉及JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)變換頁面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11webpack 如何解析代碼模塊路徑的實(shí)現(xiàn)
這篇文章主要介紹了webpack 如何解析代碼模塊路徑的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09js實(shí)現(xiàn)AI五子棋人機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)AI五子棋人機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12一款好用的移動(dòng)端滾動(dòng)插件BetterScroll
BetterScroll 是一款重點(diǎn)解決移動(dòng)端各種滾動(dòng)場(chǎng)景需求的開源插件,用于滾動(dòng)列表、選擇器、輪播圖、索引列表、開屏引導(dǎo)等應(yīng)用場(chǎng)景,感興趣的一起來了解一下2021-09-09javascript實(shí)現(xiàn)的字符串與十六進(jìn)制表示字符串相互轉(zhuǎn)換方法
這篇文章主要介紹了javascript實(shí)現(xiàn)的字符串與十六進(jìn)制表示字符串相互轉(zhuǎn)換方法,涉及javascript字符串轉(zhuǎn)換的相關(guān)技巧,在防止SQL注入和XSS中具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07