JS控制圖片翻轉(zhuǎn)示例代碼(兼容firefox,ie,chrome)
<!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>圖片旋轉(zhuǎn)效果</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<!--把下面代碼加到<head>與</head>之間-->
<style type="text/css">
ul{padding:0 15px;}
ul li{padding-bottom:15px;border-bottom:1px dashed #EEE;}
.caption{padding:15px 0 5px;}
.caption input{margin-right:10px;padding:0 10px;}
</style>
<script type="text/javascript">
/*
* www.byzuo.com
* ok!: MSIE 6, 7, 8, Firefox 3.6, chrome 4, Safari 4, Opera 10
* o 旋轉(zhuǎn)圖片ID;
* p 選擇旋轉(zhuǎn)方向,固定值為'left'或'right';
*/
function rotate(o,p){
var img = document.getElementById(o);
if(!img || !p) return false;
var n = img.getAttribute('step');
if(n== null) n=0;
if(p=='right'){
(n==3)? n=0:n++;
}else if(p=='left'){
(n==0)? n=3:n--;
}
img.setAttribute('step',n);
//MSIE
if(document.all) {
img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ n +')';
//HACK FOR MSIE 8
switch(n){
case 0:
imgimg.parentNode.style.height = img.height;
break;
case 1:
imgimg.parentNode.style.height = img.width;
break;
case 2:
imgimg.parentNode.style.height = img.height;
break;
case 3:
imgimg.parentNode.style.height = img.width;
break;
}
//DOM
}else{
var c = document.getElementById('canvas_'+o);
if(c== null){
img.style.visibility = 'hidden';
img.style.position = 'absolute';
c = document.createElement('canvas');
c.setAttribute("id",'canvas_'+o);
img.parentNode.appendChild(c);
}
var ccanvasContext = c.getContext('2d');
switch(n) {
default :
case 0 :
c.setAttribute('width', img.width);
c.setAttribute('height', img.height);
canvasContext.rotate(0 * Math.PI / 180);
canvasContext.drawImage(img, 0, 0);
break;
case 1 :
c.setAttribute('width', img.height);
c.setAttribute('height', img.width);
canvasContext.rotate(90 * Math.PI / 180);
canvasContext.drawImage(img, 0, -img.height);
break;
case 2 :
c.setAttribute('width', img.width);
c.setAttribute('height', img.height);
canvasContext.rotate(180 * Math.PI / 180);
canvasContext.drawImage(img, -img.width, -img.height);
break;
case 3 :
c.setAttribute('width', img.height);
c.setAttribute('height', img.width);
canvasContext.rotate(270 * Math.PI / 180);
canvasContext.drawImage(img, -img.width, 0);
break;
}
}
}
</script>
</head>
<body>
<!--把下面代碼加到<body>與</body>之間-->
<ul class="clearfix">
<li>
<div class="caption">
<input type="button" value="turn left" onclick="rotate('pic_1','left')">
<input type="button" value="turn right" onclick="rotate('pic_1','right')">
</div>
<div class="cont">
<img alt="javascript 圖片旋轉(zhuǎn)效果" id="pic_1" src="1.gif" alt="">
</div>
</li>
</ul>
</body>
</html>
相關(guān)文章
ES6如何將?Set?轉(zhuǎn)化為數(shù)組示例詳解
這篇文章主要為大家介紹了ES6如何將?Set?轉(zhuǎn)化為數(shù)組的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02JS實(shí)現(xiàn)簡(jiǎn)單的選擇題測(cè)評(píng)系統(tǒng)代碼思路詳解(demo)
本文給大家分享js實(shí)現(xiàn)簡(jiǎn)單的選擇題測(cè)評(píng)系統(tǒng)實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-09-09layui實(shí)現(xiàn)數(shù)據(jù)表格自定義數(shù)據(jù)項(xiàng)
今天小編就為大家分享一篇layui實(shí)現(xiàn)數(shù)據(jù)表格自定義數(shù)據(jù)項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10JS實(shí)現(xiàn)網(wǎng)頁頂部向下滑出的全國城市切換導(dǎo)航效果
這篇文章主要介紹了JS實(shí)現(xiàn)網(wǎng)頁頂部向下滑出的全國城市切換導(dǎo)航效果,涉及javascript鼠標(biāo)事件及頁面元素顯示的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08BOM系列第一篇之定時(shí)器setTimeout和setInterval
這篇文章主要介紹了BOM系列第一篇之定時(shí)器setTimeout和setInterval 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08JavaScript 用fetch 實(shí)現(xiàn)異步下載文件功能
本文通過實(shí)例代碼給大家介紹了JavaScript 用fetch 實(shí)現(xiàn)異步下載文件功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-07-07微信小程序動(dòng)態(tài)顯示項(xiàng)目倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序動(dòng)態(tài)顯示項(xiàng)目倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06利用JS實(shí)現(xiàn)AI自動(dòng)玩貪吃蛇
這篇文章主要為大家詳細(xì)介紹了如何利用JS實(shí)現(xiàn)AI版自動(dòng)玩貪吃蛇小游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JS游戲開發(fā)有一定幫助,需要的可以參考一下2022-06-06