簡單的放大縮小,顯示座標功能。
------------------------------------------------
演示程式碼:
------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 過渡//EN">
<HTML>
<頭>
<標題>文檔</標題>
<樣式類型=“文字/css”>
身體{
內邊距:0px;
邊距:0px;
背景顏色:#999999;
}
.DivMain {
位置:絕對;
文字對齊:居中;
溢出:隱藏;
}
.DivMenu {
位置:絕對;
文字對齊:左對齊;
溢出:隱藏;
}
.ImgMain {
位置:絕對;
溢出:隱藏;
邊框:0px;
}
.TBMain {
背景顏色:#CCCCCC;
內邊距:0px;
邊框:0px;
z 指數:99;
行高:18px;
}
.TBMenu {
背景顏色:#FFCCCC;
內邊距:0px;
邊框:0px;
z 指數:99;
顏色:#000000;
}
.HrScroll {
邊距:0px;
背景顏色:#FFFFFF;
內邊距:0px;
邊框:0px;
z 指數:99;
顏色:#000000;
邊框間距:0px;
寬度:160 像素;
高度:10 像素;
}
</風格>
<腳本語言=“JavaScript”>
<!--
var 原始寬度;
var 原始高度;
var objDivMain ;
var objTbMain ;
var objImg;
var ObjDrag ;
var objDivMenu ;
var objTbMemu ;
var objbtnImgInfo ;
var objDivFlag;
var objHrScroll;
var 物件;
var oTextRange ;
函數 setInit(obj) {
objDivMain = document.getElementById("divMain") ;
objTbMain = document.getElementById("TbMain") ;
objDivMenu = document.getElementById("DivMenu") ;
objTbMenu = document.getElementById("TbMenu") ;
objImg = document.getElementById("imgMain") ;
objbtnImgInfo = document.getElementById("btnImgInfo") ;
objDivFlag = document.getElementById("DivFlag");
objHrScroll = document.getElementById("HrScroll");
原寬度 = obj.寬度;
原始高度 = obj.高度;
objImg.style.width=originalWidth;
objImg.style.height = 原始高度;
fmImgZoom.FitScreen.click();
}
函數 setFitScreen() {
initImg(objImg);
}
函數設定原始(){
var intBodyWidth ;
var intBodyHeight ;
intBodyWidth = document.body.clientWidth ;
intBodyHeight = document.body.clientHeight ;
objImg.style.left = 0 ;
objImg.style.top = 0 ;
objImg.style.width=originalWidth;
objImg.style.height = 原始高度;
objImg.style.zoom = 1 ;
objDivMain.style.width = intBodyWidth ;
objDivMain.style.height = intBodyHeight - objTbMain.clientHeight;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}
函數 initImg(objCurrent) {
var intBodyWidth ;
var intBodyHeight ;
intBodyWidth = document.body.clientWidth ;
intBodyHeight = document.body.clientHeight ;
objTbMain.style.top = 0;
objTbMain.style.height = 20;
objTbMain.style.width = intBodyWidth;
objDivMain.style.left = 0 ;
objDivMain.style.height = intBodyHeight - objTbMain.clientHeight;
objDivMain.style.width = intBodyWidth ;
initZoom(objCurrent);
}
函數 initZoom(obj){
var intObjWidth ;
var intObjHeight ;
var intDivHeight ;
var intZoomRatio ;
intDivHeight = objDivMain.style.pixelHeight;
intObjHeight = obj.style.pixelHeight;
intZoomRatio = intDivHeight / intObjHeight;
obj.style.zoom = intZoomRatio ;
obj.style.top = 0;
obj.style.left = 0;
fnWritePos (obj.style.left,obj.style.top,obj.style.zoom) ;
}
函數 fnMouseDown(obj) {
ObjDrag=obj;
ObjDrag.setCapture();
ObjDrag.l=event.x-ObjDrag.style.pixelLeft;
ObjDrag.t=event.y-ObjDrag.style.pixelTop;
}
函數 fnMouseMove() {
if(ObjDrag!=null) {
ObjDrag.style.left = event.x-ObjDrag.l;
ObjDrag.style.top = event.y-ObjDrag.t;
fnWritePos(ObjDrag.style.left,ObjDrag.style.top,ObjDrag.style.zoom);
}
}
函數 fnMouseUp() {
if(ObjDrag!=null) {
ObjDrag.releaseCapture();
ObjDrag=空;
}
}
函數 fnZoomIn() {
objImg.style.zoom = parseFloat(objImg.style.zoom) + 0.01 ;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}
函數 fnZoomOut() {
if(objImg.style.zoom>0.01){
objImg.style.zoom = parseFloat(objImg.style.zoom) - 0.01 ;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}
}
函數 fnDivMouseDown() {
if ( event.button == 2 && event.srcElement.id != "imgMain") {
objDivMenu.style.top = event.y;
objDivMenu.style.left = event.x;
objDivMenu.style.visibility = "";
oTextRange = objHrScroll.createTextRange() ;
oRcts = oTextRange.getClientRects();
oBndRct = objDivFlag.getBoundingClientRect();
objDivFlag.style.top = oRcts[0].top;
objDivFlag.style.left = oRcts[0].left + parseInt(objImg.style.zoom * 10) ;
objDivFlag.style.visibility = "";
}
if ( event.button == 1 && event.srcElement.id != "imgMain") {
objDivMenu.style.visibility = "隱藏";
objDivFlag.style.visibility = "隱藏";
}
}
函數 fnWritePos(intLeft,intTop,intZoom) {
objbtnImgInfo.innerText = " ImgLeft:" + intLeft + " ImgTop:" + intTop + " ImgZoom:" + intZoom ;
}
函數 fnSlipMouseDown(obj) {
ObjDrag=obj;
ObjDrag.setCapture();
ObjDrag.l = event.x - ObjDrag.style.pixelLeft;
ObjDrag.t = ObjDrag.style.pixelTop;
}
函數 fnSlipMouseMove() {
if(ObjDrag!=null) {
if(event.x >= oRcts[0].left && event.x <= oRcts[0].right ){
ObjDrag.style.left = event.x - objDivFlag.offsetWidth / 2;
}
ObjDrag.t = ObjDrag.style.pixelTop;
var intCurrentPos = ObjDrag.style.pixelLeft - oRcts[0].left + objDivFlag.offsetWidth / 2;
var intPicZoom = intCurrentPos / 10;
objImg.style.zoom = intPicZoom;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}
}
函數 fnSlipMouseUp() {
if(ObjDrag!=null) {
ObjDrag.releaseCapture();
ObjDrag=空;
}
}
函數設定顯示(){
if(objImg.style.visibility == ""){
objImg.style.visibility = "隱藏";
}
別的{
objImg.style.visibility = "";
}
}
函數 fnContextMenu() {
window.event.returnValue=false;
}
//-->
</腳本>
</頭>
<BODY oncontextmenu="fnContextMenu()">
<表單名稱=“fmImgZoom”>
<表id =“tbMain”類別=“TBMain”>
<TR>
<TD>
<input type="button" name="ZoomIn" value="ZoomIn" onclick="fnZoomIn()">
<input type="button" name="ZoomOut" value="ZoomOut" onclick="fnZoomOut()">
<input type="button" name="Original" value="Original" onclick="setOriginal()">
<input type="button" name="FitScreen" value="FitScreen" onclick="setFitScreen()">
<input type="button" name="btnView" value="隱藏/視圖" onclick="setDisplay()">
<輸入id =“btnImgInfo”大小=“50”>
</TD>
</TR>
</表>
<div id="divMain" class="DivMain" onmousedown="fnDivMouseDown()"><img src="<A href="/u/info_img/2009-06/08/22.jpg" id="imgMain" class="imgMain" onload="setInit(this)" onmousedown="fnMouseDown(this)" onmousemove="fnMouseMove()" onmouseup="fnMouseUp()" ></div>
<div id="divMenu" class="DivMenu" style="visibility:hidden">
<表類別=“TBMenu”>
<TR>
<TD>10%</TD>
<TD><button id="HrScroll" class="HrScroll"><hr noshade></button> </TD>
<TD>1600%</TD>
</TR>
</表>
</div>
<DIV ID="DivFlag" STYLE="位置:絕對;可見性:隱藏;寬度:1px;高度:10px;z-index:99" onmousedown="fnSlipMouseDown(this)" onmousemove="fnSlipMouseMove()" onmouseup= " fnSlipMouseUp()" onmouseover="this.style.cursor ='hand'" onmouseout="this.style.cursor=''">↑</DIV>
</形式>
</正文>
</HTML>