国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

JS控制圖片翻轉(zhuǎn)示例代碼(兼容firefox,ie,chrome)

2019-11-20 21:25:54
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
復(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>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 寻甸| 杂多县| 洱源县| 赤水市| 兴和县| 闸北区| 卫辉市| 二连浩特市| 怀远县| 岐山县| 台东市| 尤溪县| 伊川县| 山西省| 章丘市| 洛宁县| 绍兴市| 琼中| 公安县| 耒阳市| 万州区| 炎陵县| 上高县| 婺源县| 永昌县| 新余市| 石嘴山市| 嵊泗县| 焦作市| 宝坻区| 珲春市| 陵水| 东台市| 锦州市| 蒙山县| 德钦县| 琼海市| 奉节县| 玉环县| 汉源县| 伊川县|