JS控制圖片翻轉(zhuǎn)示例代碼(兼容firefox,ie,chrome)
更新時間:2013年12月19日 09:10:37 作者:
本篇文章主要介紹了JS控制圖片翻轉(zhuǎn)示例代碼(兼容firefox,ie,chrome) 需要的朋友可以過來參考下,希望對大家有所幫助
復(fù)制代碼 代碼如下:
<!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)文章
JavaScript 監(jiān)控微信瀏覽器且自帶返回按鈕時間
這篇文章主要介紹了JavaScript 監(jiān)控微信瀏覽器且自帶返回按鈕時間的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11
Javascript中arguments用法實(shí)例分析
這篇文章主要介紹了Javascript中arguments用法,實(shí)例分析了javascript利用arguments實(shí)現(xiàn)模擬重載功能,需要的朋友可以參考下2015-06-06
微信小程序iBeacon測距及穩(wěn)定程序的實(shí)現(xiàn)解析
這篇文章主要介紹了微信小程序iBeacon測距及穩(wěn)定程序的實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
一文詳解requestAnimationFrame請求動畫幀
requestAnimationFrame是一個用于動畫效果的 API,它使用瀏覽器的刷新率來執(zhí)行回調(diào)函數(shù),通常每秒鐘執(zhí)行60次,這篇文章主要給大家介紹了關(guān)于requestAnimationFrame請求動畫幀的相關(guān)資料,需要的朋友可以參考下2023-12-12
正則中的回溯定義與用法分析【JS與java實(shí)現(xiàn)】
這篇文章主要介紹了正則中的回溯定義與用法,結(jié)合實(shí)例形式分析了回溯的概念、功能并提供了JS與java實(shí)現(xiàn)方法,需要的朋友可以參考下2016-12-12
JavaScript之Getters和Setters 平臺支持等詳細(xì)介紹
現(xiàn)在,JavaScript的Getters和Setters使用非常廣泛,它和每個JavaScript開發(fā)者的切身利益息息相關(guān),我們先來快速了解什么是Getters和Setters,以及它們?yōu)槭裁春苡杏?然后,我們來看看現(xiàn)在都有哪些平臺支持Gettets和Setters2012-12-12

