JS簡(jiǎn)單的圖片放大縮小的兩種方法
更新時(shí)間:2013年11月11日 16:38:29 作者:
這篇文章介紹了JS簡(jiǎn)單的圖片放大縮小的兩種方法,有需要的朋友可以參考一下
以左上角為定點(diǎn),放大縮小,該點(diǎn)位置不變。
方法一:
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逆時(shí)針" name="RotateL" id="RotateL" onclick="rotateRight('oImg',90);"> -->
</form>
方法二:
CSS編碼如下:
Css代碼
#biankuang{height:480px;width:320px;margin: 30px auto;}//加一個(gè)border可以看到定點(diǎn)為左上角。
下面是實(shí)現(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>
方法一:
Html代碼
復(fù)制代碼 代碼如下:
<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逆時(shí)針" name="RotateL" id="RotateL" onclick="rotateRight('oImg',90);"> -->
</form>
方法二:
CSS編碼如下:
Css代碼
復(fù)制代碼 代碼如下:
#biankuang{height:480px;width:320px;margin: 30px auto;}//加一個(gè)border可以看到定點(diǎn)為左上角。
下面是實(shí)現(xiàn)圖片縮小放大功能的JS代碼:
Js代碼
復(fù)制代碼 代碼如下:
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代碼
復(fù)制代碼 代碼如下:
<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>
您可能感興趣的文章:
- js放大鏡放大圖片效果
- js實(shí)現(xiàn)單擊圖片放大圖片的方法
- js實(shí)現(xiàn)點(diǎn)擊圖片在屏幕中間彈出放大效果
- JS實(shí)現(xiàn)鼠標(biāo)移動(dòng)到縮略圖顯示大圖的圖片放大效果
- js實(shí)現(xiàn)圖片放大展示效果
- 鼠標(biāo)滑上去后圖片放大浮出效果的js代碼
- JavaScript圖片放大鏡效果代碼[代碼比較簡(jiǎn)單]
- 手機(jī)端 HTML5使用photoswipe.js仿微信朋友圈圖片放大效果
- JS網(wǎng)頁(yè)圖片查看器(兼容IE、FF)可控制圖片放大縮小移動(dòng)
- JavaScript實(shí)現(xiàn)淘寶網(wǎng)圖片的局部放大功能
相關(guān)文章
微信小程序接入微信支付實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了微信小程序接入微信支付實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12
JavaScript實(shí)現(xiàn)通過(guò)滑塊改變網(wǎng)頁(yè)顏色
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)通過(guò)滑塊改變網(wǎng)頁(yè)顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
關(guān)于promise.all()的使用及說(shuō)明
這篇文章主要介紹了關(guān)于promise.all()的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
文本框(input)獲取焦點(diǎn)(onfocus)時(shí)樣式改變的示例代碼
本篇文章主要是對(duì)文本框(input)獲取焦點(diǎn)(onfocus)時(shí)樣式改變的示例代碼進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01

