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

首頁 > 編程 > JavaScript > 正文

JS簡單的圖片放大縮小的兩種方法

2019-11-20 21:44:11
字體:
來源:轉載
供稿:網(wǎng)友
以左上角為定點,放大縮小,該點位置不變。

方法一:

Html代碼
復制代碼 代碼如下:

   <script type="text/javascript">
        //兼容IE和火狐   縮小放大、縮放
        function ImageSuofang(args) {
            var oImg = document.getElementById("oImg");
            if (args) {
                oImgoImg.width = oImg.width * 1.1;
                oImgoImg.height = oImg.height * 1.1;
            }
            else {
                oImgoImg.width = oImg.width / 1.1;
                oImgoImg.height = oImg.height / 1.1;
            }
        }    
     </script>

<form id="form1">

     <div class="pancontainer" data-orient="center" style="width:320px; height:480px;margin: 5px 300px 0px 400px;border: 1px solid #000;">
<img id="oImg" src="/img/c.jpg" alt="pic"/>
</div>

             <input id="btn1" type="button" value="放大" onclick="ImageSuofang(true)" />
        <input id="btn2" type="button" value="縮小" onclick="ImageSuofang(false)" />
         <!--            <input type="button" value="<-Rotate逆時針" name="RotateL" id="RotateL" onclick="rotateRight('oImg',90);">  -->

            
</form>

方法二:

CSS編碼如下:

Css代碼
復制代碼 代碼如下:

#biankuang{height:480px;width:320px;margin: 30px auto;}//加一個border可以看到定點為左上角。

下面是實現(xiàn)圖片縮小放大功能的JS代碼:

Js代碼
復制代碼 代碼如下:

var zoomLevel = 0;
var currentWidth = 0;
var currentHeight = 0;
var originalWidth = 0;
var originalHeight = 0;
function initial(){
    currentWidth = document.myImage.width;
    currentHeight = document.myImage.height;
    originalWidth = currentWidth;
    originalHeight = currentHeight;
    update();
}
function zoomIn(){
    document.myImage.width = currentWidth*1.2;
    document.myImage.height = currentHeight*1.2;
    zoomLevel = zoomLevel + 1;
    update();
}
function zoomOut(){
    document.myImage.width = currentWidth/1.2;
    document.myImage.height = currentHeight/1.2;
    zoomLevel = zoomLevel - 1;
    update();
}
function resetImage(){
    document.myImage.width = originalWidth;
    document.myImage.height = originalHeight;
    zoomLevel = 0;
    update();
}
function update(){
    currentWidth = document.myImage.width;
    currentHeight = document.myImage.height;
    zoomsize.innerText = zoomLevel;
    imgsize.innerText = currentWidth + "X" + currentHeight;
}

 html的body中的代碼如下:

Html代碼
復制代碼 代碼如下:

<body onload="initial()">

<div id="biankuang" data-orient="center">
<img name="myImage" src="/img/c.jpg" alt="pic"/>     //引入本地圖片
</div>

<p>
<input type="button" value="放大圖片" onclick="zoomIn()">
<input type="button" value="縮小圖片" onclick="zoomOut()">
<input type="button" value="重置圖片" onclick="resetImage()">
<span id="zoomsize"></span> <span id="imgsize"></span></p>
</body>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 平定县| 曲靖市| 博客| 嵊州市| 绵阳市| 依安县| 涟水县| 甘孜县| 桐柏县| 怀仁县| 南平市| 尼玛县| 错那县| 吴堡县| 镇宁| 澎湖县| 会理县| 宁都县| 秦皇岛市| 沙洋县| 莱西市| 东丽区| 彭山县| 左权县| 衡东县| 蛟河市| 海晏县| 兴义市| 资溪县| 垣曲县| 璧山县| 舒城县| 镇坪县| 龙门县| 政和县| 边坝县| 肃宁县| 文水县| 灵丘县| 青岛市| 游戏|