好不容易成兩星,散分散分,順便請高手幫我修改下代碼。 右鍵是左右移動,調節ZOOM,讓圖片放大縮小的功能。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 右键的textrange區域和按鈕的實際區域有所區別,怎樣能夠控制他們完全的坐標一致?這個就是我需要高手來解答的。 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> Document </TITLE><style type="text/css">body{ padding : 0px ; margin : 0px; background-color : #999999 ;}.DivMain { position : absolute ; text-align : center ; overflow : hidden ;}.DivMenu { position : absolute ; text-align : left ; overflow : hidden ;}.ImgMain { position : absolute ; overflow : hidden ; border : 0px ;}.TBMain { background-color : #CCCCCC ; padding : 0px ; border : 0px ; z-index : 99 ; line-height : 18px ; }.TBMenu { background-color : #FFCCCC ; padding : 0px ; border : 0px ; z-index : 99 ; color: #000000 ; }.HrScroll { margin : 0px; background-color : #FFFFFF ; padding : 0px ; border : 0px ; z-index : 99 ; color : #000000 ; border-spacing : 0px; width : 160px ; height : 10px ;}</style><SCRIPT LANGUAGE="JavaScript"><!-- var originalWidth ; var originalHeight ; var objDivMain ; var objTbMain ; var objImg ; var ObjDrag ; var objDivMenu ; var objTbMemu ; var objbtnImgInfo ; var objDivFlag; var objHrScroll; var oRcts ; var oTextRange ; function 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"); originalWidth = obj.width; originalHeight = obj.height; objImg.style.width = originalWidth; objImg.style.height = originalHeight; fmImgZoom.FitScreen.click();}function setFitScreen() { initImg(objImg);}function setOriginal() { 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 = originalHeight; objImg.style.zoom = 1 ; objDivMain.style.width = intBodyWidth ; objDivMain.style.height = intBodyHeight - objTbMain.clientHeight; fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);}function 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);}function 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) ;}function fnMouseDown(obj) { ObjDrag=obj; ObjDrag.setCapture(); ObjDrag.l=event.x-ObjDrag.style.pixelLeft; ObjDrag.t=event.y-ObjDrag.style.pixelTop;}function 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); }}function fnMouseUp() { if(ObjDrag!=null) { ObjDrag.releaseCapture(); ObjDrag=null; }}function fnZoomIn() { objImg.style.zoom = parseFloat(objImg.style.zoom) + 0.01 ; fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);}function 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); }}function 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 = "hidden"; objDivFlag.style.visibility = "hidden"; }}function fnWritePos(intLeft,intTop,intZoom) { objbtnImgInfo.innerText = " ImgLeft:" + intLeft + " ImgTop:" + intTop + " ImgZoom:" + intZoom ;}function fnSlipMouseDown(obj) { ObjDrag=obj; ObjDrag.setCapture(); ObjDrag.l = event.x - ObjDrag.style.pixelLeft; ObjDrag.t = ObjDrag.style.pixelTop;}function fnSlipMouseMove() { if(ObjDrag!=null) { if(event.x - ObjDrag.l >= oRcts[0].left && event.x-ObjDrag.l <= oRcts[0].right ){ ObjDrag.style.left = event.x - ObjDrag.l; } ObjDrag.t = ObjDrag.style.pixelTop; var intCurrentPos = ObjDrag.style.pixelLeft - oRcts[0].left ; var intPicZoom = intCurrentPos / 10; objImg.style.zoom = intPicZoom; fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom); }}function fnSlipMouseUp() { if(ObjDrag!=null) { ObjDrag.releaseCapture(); ObjDrag=null; }}function setDisplay() { if(objImg.style.visibility == ""){ objImg.style.visibility = "hidden"; } else{ objImg.style.visibility = ""; }}function fnContextMenu() { window.event.returnValue=false;}//--></SCRIPT></HEAD><BODY oncontextmenu="fnContextMenu()"><form name="fmImgZoom"><TABLE id="tbMain" class="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="hide/view" onclick="setDisplay()"> <input id="btnImgInfo" size="50"> </TD></TR></TABLE><div id="divMain" class="DivMain" onmousedown="fnDivMouseDown()"><img src="http://www.dayu88.net/images/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"><TABLE class="TBMenu"><TR> <TD>1%</TD> <TD><button id="HrScroll" class="HrScroll"><hr noshade></button> </TD> <TD>1600%</TD></TR></TABLE></div><DIV ID="DivFlag" STYLE="position:absolute;visibility:hidden;width:1px;height:10px;z-index:99" onmousedown="fnSlipMouseDown(this)" onmousemove="fnSlipMouseMove()" onmouseup="fnSlipMouseUp()" onmouseover="this.style.cursor ='hand'" onmouseout="this.style.cursor=''">↑</DIV></form></BODY></HTML> 回复人: fantiny(虽然我是菜鸟,不过我是~~非暴力不合作~~的菜鸟) ( ) 信誉:99 2005-10-31 11:03:00 得分: 0 右键的textrange區域和按鈕的實際區域有所區別,怎樣能夠控制他們完全的坐標一致?這個就是我需要高手來解答的。 ====================================================== 其实应该是一致的,至少 left, right 没有问题,是你下面的判断语句有问题,第238行,把if(event.x - ObjDrag.l >= oRcts[0].left && event.x-ObjDrag.l <= oRcts[0].right )改成:if(event.x >= oRcts[0].left && event.x <= oRcts[0].right )我试过了,很正常。 另外,没有必要使用 TextRange 啊,TextRange 这东西用起来总是不那么放心,用下面的就应该没问题了,下面是在你的代码基础上修改的,没有仔细看你其他部分的代码,就直接生成了 oRcts 数组,你可以作适当简化:第208-210行改为:oRcts = Array(new Object());oRcts[0].top = fnGetTop(objHrScroll);oRcts[0].left = fnGetLeft(objHrScroll);oRcts[0].right = oRcts[0].left + objHrScroll.clientWidth;oRcts[0].bottom = oRcts[0].top + objHrScroll.clientHeight;另外添两个函数:function fnGetTop(obj) { var value = 0; for( ; obj!=null; obj=obj.offsetParent) value += obj.offsetTop; return value;}function fnGetLeft(obj) { var value = 0; for( ; obj!=null; obj=obj.offsetParent) value += obj.offsetLeft; return value;} if(event.x >= oRcts[0].left && event.x <= oRcts[0].right )------------------------還是有偏移的情況出現。這種情況是左邊偏移了一點。=============================================================另外,没有必要使用 TextRange 啊,TextRange 这东西用起来总是不那么放心,----------------------------我是故意用textRange的.其實我主要就是爲了測試下它的用法才寫的。 今天刚把公司的一个技术问题解决了,心情爽啊。fantiny兄文档王子 又共享代码拉。郁闷,晚上再看,这个地址居然被公司的网络屏蔽掉了“http://www.dayu88.net/images/22.jpg” 你仔細看zoom的變化會發現,其實你的那種方法還是沒有解釋爲什麽會出現偏移,以及真正怎麽解決那種偏移。 还有一个问题,拖动时拖动时你没有把“↑”的宽度计算进去,因此,只要把函数 fnSlipMouseMove 修改为下面的形式就没有问题了。function 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); }} 完整版本,當然裏面還有些bug,需要用的人自己改吧。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> Document </TITLE><style type="text/css">body{ padding : 0px ; margin : 0px; background-color : #999999 ;}.DivMain { position : absolute ; text-align : center ; overflow : hidden ;}.DivMenu { position : absolute ; text-align : left ; overflow : hidden ;}.ImgMain { position : absolute ; overflow : hidden ; border : 0px ;}.TBMain { background-color : #CCCCCC ; padding : 0px ; border : 0px ; z-index : 99 ; line-height : 18px ; }.TBMenu { background-color : #FFCCCC ; padding : 0px ; border : 0px ; z-index : 99 ; color: #000000 ; }.HrScroll { margin : 0px; background-color : #FFFFFF ; padding : 0px ; border : 0px ; z-index : 99 ; color : #000000 ; border-spacing : 0px; width : 160px ; height : 10px ;}</style><SCRIPT LANGUAGE="JavaScript"><!-- var originalWidth ; var originalHeight ; var objDivMain ; var objTbMain ; var objImg ; var ObjDrag ; var objDivMenu ; var objTbMemu ; var objbtnImgInfo ; var objDivFlag; var objHrScroll; var oRcts ; var oTextRange ; function 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"); originalWidth = obj.width; originalHeight = obj.height; objImg.style.width = originalWidth; objImg.style.height = originalHeight; fmImgZoom.FitScreen.click();}function setFitScreen() { initImg(objImg);}function setOriginal() { 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 = originalHeight; objImg.style.zoom = 1 ; objDivMain.style.width = intBodyWidth ; objDivMain.style.height = intBodyHeight - objTbMain.clientHeight; fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);}function 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);}function 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) ;}function fnMouseDown(obj) { ObjDrag=obj; ObjDrag.setCapture(); ObjDrag.l=event.x-ObjDrag.style.pixelLeft; ObjDrag.t=event.y-ObjDrag.style.pixelTop;}function 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); }}function fnMouseUp() { if(ObjDrag!=null) { ObjDrag.releaseCapture(); ObjDrag=null; }}function fnZoomIn() { objImg.style.zoom = parseFloat(objImg.style.zoom) + 0.01 ; fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);}function 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); }}function 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 = "hidden"; objDivFlag.style.visibility = "hidden"; }}function fnWritePos(intLeft,intTop,intZoom) { objbtnImgInfo.innerText = " ImgLeft:" + intLeft + " ImgTop:" + intTop + " ImgZoom:" + intZoom ;}function fnSlipMouseDown(obj) { ObjDrag=obj; ObjDrag.setCapture(); ObjDrag.l = event.x - ObjDrag.style.pixelLeft; ObjDrag.t = ObjDrag.style.pixelTop;}function 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); }}function fnSlipMouseUp() { if(ObjDrag!=null) { ObjDrag.releaseCapture(); ObjDrag=null; }}function setDisplay() { if(objImg.style.visibility == ""){ objImg.style.visibility = "hidden"; } else{ objImg.style.visibility = ""; }}function fnContextMenu() { window.event.returnValue=false;}//--></SCRIPT></HEAD><BODY oncontextmenu="fnContextMenu()"><form name="fmImgZoom"><TABLE id="tbMain" class="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="hide/view" onclick="setDisplay()"> <input id="btnImgInfo" size="50"> </TD></TR></TABLE><div id="divMain" class="DivMain" onmousedown="fnDivMouseDown()"><img src="http://www.dayu88.net/images/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"><TABLE class="TBMenu"><TR> <TD>10%</TD> <TD><button id="HrScroll" class="HrScroll"><hr noshade></button> </TD> <TD>1600%</TD></TR></TABLE></div><DIV ID="DivFlag" STYLE="position:absolute;visibility:hidden;width:1px;height:10px;z-index:99" onmousedown="fnSlipMouseDown(this)" onmousemove="fnSlipMouseMove()" onmouseup="fnSlipMouseUp()" onmouseover="this.style.cursor ='hand'" onmouseout="this.style.cursor=''">↑</DIV></form></BODY></HTML> 把拖动和滑块代码放到HTC里面,网页里面的代码就少多了,而且也容易理解一些。 还有,里面的 document.getElementById 全都是多余的,直接使用 id 访问 HTML 元素就可以了,没必要放到全局变量里面。 请问接口、子类和类以及对象时啥关系?它能在编程中起到啥作用? ShowDialog框架文件弹出并返回值到镶嵌框架的文件中 js 如何打开本地word程序 word文档也是本地.不是在浏览器中打开 通过iframe内嵌浏览ppt/doc等文件无法充满整个iframe框架,求教高手解答,高分回报!!! 有了jquery 为什么还要用extjs 关于highchart的多曲线的实时刷新【功能实现但效果怪怪的 好像心电图、、】求帮忙看下问题 Jquery用json调用wcf服务的问题 打印问题? 问一个简单的问题!为何提示错误说缺少对象?? 求助 求助一个小问题 父窗口操作子窗口的问题
這個就是我需要高手來解答的。
<HTML>
<HEAD>
<TITLE> Document </TITLE>
<style type="text/css">
body{
padding : 0px ;
margin : 0px;
background-color : #999999 ;}
.DivMain {
position : absolute ;
text-align : center ;
overflow : hidden ;
}
.DivMenu {
position : absolute ;
text-align : left ;
overflow : hidden ;
}
.ImgMain {
position : absolute ;
overflow : hidden ;
border : 0px ;
}
.TBMain {
background-color : #CCCCCC ;
padding : 0px ;
border : 0px ;
z-index : 99 ;
line-height : 18px ;
}
.TBMenu {
background-color : #FFCCCC ;
padding : 0px ;
border : 0px ;
z-index : 99 ;
color: #000000 ;
}
.HrScroll {
margin : 0px;
background-color : #FFFFFF ;
padding : 0px ;
border : 0px ;
z-index : 99 ;
color : #000000 ;
border-spacing : 0px;
width : 160px ;
height : 10px ;
}
</style><SCRIPT LANGUAGE="JavaScript">
<!-- var originalWidth ;
var originalHeight ; var objDivMain ;
var objTbMain ; var objImg ;
var ObjDrag ; var objDivMenu ;
var objTbMemu ; var objbtnImgInfo ; var objDivFlag;
var objHrScroll; var oRcts ;
var oTextRange ; function 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"); originalWidth = obj.width;
originalHeight = obj.height; objImg.style.width = originalWidth;
objImg.style.height = originalHeight; fmImgZoom.FitScreen.click();
}function setFitScreen() { initImg(objImg);
}function setOriginal() { 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 = originalHeight;
objImg.style.zoom = 1 ; objDivMain.style.width = intBodyWidth ;
objDivMain.style.height = intBodyHeight - objTbMain.clientHeight; fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}function 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);
}function 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) ;
}function fnMouseDown(obj) {
ObjDrag=obj;
ObjDrag.setCapture();
ObjDrag.l=event.x-ObjDrag.style.pixelLeft;
ObjDrag.t=event.y-ObjDrag.style.pixelTop;
}function 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);
}
}function fnMouseUp() {
if(ObjDrag!=null) {
ObjDrag.releaseCapture();
ObjDrag=null;
}
}function fnZoomIn() { objImg.style.zoom = parseFloat(objImg.style.zoom) + 0.01 ;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}function 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);
}
}function 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 = "hidden";
objDivFlag.style.visibility = "hidden";
}
}function fnWritePos(intLeft,intTop,intZoom) {
objbtnImgInfo.innerText = " ImgLeft:" + intLeft + " ImgTop:" + intTop + " ImgZoom:" + intZoom ;
}function fnSlipMouseDown(obj) {
ObjDrag=obj;
ObjDrag.setCapture();
ObjDrag.l = event.x - ObjDrag.style.pixelLeft;
ObjDrag.t = ObjDrag.style.pixelTop;
}function fnSlipMouseMove() {
if(ObjDrag!=null) { if(event.x - ObjDrag.l >= oRcts[0].left && event.x-ObjDrag.l <= oRcts[0].right ){
ObjDrag.style.left = event.x - ObjDrag.l;
} ObjDrag.t = ObjDrag.style.pixelTop;
var intCurrentPos = ObjDrag.style.pixelLeft - oRcts[0].left ;
var intPicZoom = intCurrentPos / 10;
objImg.style.zoom = intPicZoom;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}
}function fnSlipMouseUp() {
if(ObjDrag!=null) {
ObjDrag.releaseCapture();
ObjDrag=null;
}
}
function setDisplay() { if(objImg.style.visibility == ""){
objImg.style.visibility = "hidden";
}
else{
objImg.style.visibility = "";
}
}function fnContextMenu() {
window.event.returnValue=false;
}
//-->
</SCRIPT>
</HEAD><BODY oncontextmenu="fnContextMenu()">
<form name="fmImgZoom">
<TABLE id="tbMain" class="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="hide/view" onclick="setDisplay()">
<input id="btnImgInfo" size="50">
</TD>
</TR>
</TABLE>
<div id="divMain" class="DivMain" onmousedown="fnDivMouseDown()"><img src="http://www.dayu88.net/images/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">
<TABLE class="TBMenu">
<TR>
<TD>1%</TD>
<TD><button id="HrScroll" class="HrScroll"><hr noshade></button> </TD>
<TD>1600%</TD>
</TR>
</TABLE>
</div>
<DIV ID="DivFlag" STYLE="position:absolute;visibility:hidden;width:1px;height:10px;z-index:99" onmousedown="fnSlipMouseDown(this)" onmousemove="fnSlipMouseMove()" onmouseup="fnSlipMouseUp()" onmouseover="this.style.cursor ='hand'" onmouseout="this.style.cursor=''">↑</DIV>
</form>
</BODY>
</HTML>
右键的textrange區域和按鈕的實際區域有所區別,怎樣能夠控制他們完全的坐標一致?
這個就是我需要高手來解答的。
====================================================== 其实应该是一致的,至少 left, right 没有问题,是你下面的判断语句有问题,第238行,把if(event.x - ObjDrag.l >= oRcts[0].left && event.x-ObjDrag.l <= oRcts[0].right )改成:if(event.x >= oRcts[0].left && event.x <= oRcts[0].right )我试过了,很正常。
用下面的就应该没问题了,下面是在你的代码基础上修改的,没有仔细看你其他
部分的代码,就直接生成了 oRcts 数组,你可以作适当简化:第208-210行改为:oRcts = Array(new Object());
oRcts[0].top = fnGetTop(objHrScroll);
oRcts[0].left = fnGetLeft(objHrScroll);
oRcts[0].right = oRcts[0].left + objHrScroll.clientWidth;
oRcts[0].bottom = oRcts[0].top + objHrScroll.clientHeight;另外添两个函数:
function fnGetTop(obj) {
var value = 0;
for( ; obj!=null; obj=obj.offsetParent) value += obj.offsetTop;
return value;
}
function fnGetLeft(obj) {
var value = 0;
for( ; obj!=null; obj=obj.offsetParent) value += obj.offsetLeft;
return value;
}
------------------------
還是有偏移的情況出現。這種情況是左邊偏移了一點。=============================================================另外,没有必要使用 TextRange 啊,TextRange 这东西用起来总是不那么放心,
----------------------------
我是故意用textRange的.其實我主要就是爲了測試下它的用法才寫的。
fantiny兄文档王子 又共享代码拉。
郁闷,晚上再看,这个地址居然被公司的网络屏蔽掉了“http://www.dayu88.net/images/22.jpg”
因此,只要把函数 fnSlipMouseMove 修改为下面的形式就没有问题了。function 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);
}
}
<HTML>
<HEAD>
<TITLE> Document </TITLE>
<style type="text/css">
body{
padding : 0px ;
margin : 0px;
background-color : #999999 ;}
.DivMain {
position : absolute ;
text-align : center ;
overflow : hidden ;
}
.DivMenu {
position : absolute ;
text-align : left ;
overflow : hidden ;
}
.ImgMain {
position : absolute ;
overflow : hidden ;
border : 0px ;
}
.TBMain {
background-color : #CCCCCC ;
padding : 0px ;
border : 0px ;
z-index : 99 ;
line-height : 18px ;
}
.TBMenu {
background-color : #FFCCCC ;
padding : 0px ;
border : 0px ;
z-index : 99 ;
color: #000000 ;
}
.HrScroll {
margin : 0px;
background-color : #FFFFFF ;
padding : 0px ;
border : 0px ;
z-index : 99 ;
color : #000000 ;
border-spacing : 0px;
width : 160px ;
height : 10px ;
}
</style><SCRIPT LANGUAGE="JavaScript">
<!-- var originalWidth ;
var originalHeight ; var objDivMain ;
var objTbMain ; var objImg ;
var ObjDrag ; var objDivMenu ;
var objTbMemu ; var objbtnImgInfo ; var objDivFlag;
var objHrScroll; var oRcts ;
var oTextRange ; function 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"); originalWidth = obj.width;
originalHeight = obj.height; objImg.style.width = originalWidth;
objImg.style.height = originalHeight; fmImgZoom.FitScreen.click();
}function setFitScreen() { initImg(objImg);
}function setOriginal() { 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 = originalHeight;
objImg.style.zoom = 1 ; objDivMain.style.width = intBodyWidth ;
objDivMain.style.height = intBodyHeight - objTbMain.clientHeight; fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}function 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);
}function 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) ;
}function fnMouseDown(obj) {
ObjDrag=obj;
ObjDrag.setCapture();
ObjDrag.l=event.x-ObjDrag.style.pixelLeft;
ObjDrag.t=event.y-ObjDrag.style.pixelTop;
}function 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);
}
}function fnMouseUp() {
if(ObjDrag!=null) {
ObjDrag.releaseCapture();
ObjDrag=null;
}
}function fnZoomIn() { objImg.style.zoom = parseFloat(objImg.style.zoom) + 0.01 ;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}function 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);
}
}function 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 = "hidden";
objDivFlag.style.visibility = "hidden";
}
}function fnWritePos(intLeft,intTop,intZoom) {
objbtnImgInfo.innerText = " ImgLeft:" + intLeft + " ImgTop:" + intTop + " ImgZoom:" + intZoom ;
}function fnSlipMouseDown(obj) {
ObjDrag=obj;
ObjDrag.setCapture();
ObjDrag.l = event.x - ObjDrag.style.pixelLeft;
ObjDrag.t = ObjDrag.style.pixelTop;
}function 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);
}
}function fnSlipMouseUp() {
if(ObjDrag!=null) {
ObjDrag.releaseCapture();
ObjDrag=null;
}
}
function setDisplay() { if(objImg.style.visibility == ""){
objImg.style.visibility = "hidden";
}
else{
objImg.style.visibility = "";
}
}function fnContextMenu() {
window.event.returnValue=false;
}
//-->
</SCRIPT>
</HEAD><BODY oncontextmenu="fnContextMenu()">
<form name="fmImgZoom">
<TABLE id="tbMain" class="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="hide/view" onclick="setDisplay()">
<input id="btnImgInfo" size="50">
</TD>
</TR>
</TABLE>
<div id="divMain" class="DivMain" onmousedown="fnDivMouseDown()"><img src="http://www.dayu88.net/images/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">
<TABLE class="TBMenu">
<TR>
<TD>10%</TD>
<TD><button id="HrScroll" class="HrScroll"><hr noshade></button> </TD>
<TD>1600%</TD>
</TR>
</TABLE>
</div>
<DIV ID="DivFlag" STYLE="position:absolute;visibility:hidden;width:1px;height:10px;z-index:99" onmousedown="fnSlipMouseDown(this)" onmousemove="fnSlipMouseMove()" onmouseup="fnSlipMouseUp()" onmouseover="this.style.cursor ='hand'" onmouseout="this.style.cursor=''">↑</DIV>
</form>
</BODY>
</HTML>