Well, thanks for that, but what I'm looking for is just for a solution in regards to the zooming part, the box itself is fine.
Main Topics
Browse All TopicsI've been working for a while on this code and I need a second pair of eyes here.
The idea is as follow :
1. Save the Code in an HTML file.
2. Open in browser.
3. Click the [Drag/Zoom] Icon, change it to Zoom.
4. Click anywhere on the screen and try to zoom into the elephants eye.
You will notice that after 5+ zoom activities, you will lose your target.
Half of the code is a "debug" code, so you can ignore it.
Also, if anyone has a ready made code to receive the same effect (IE5.5+ only) I will consider this as an answer.
--------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Blank:Document </TITLE>
<META NAME="Author" CONTENT="(C)Avner Cohen">
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
var initX="";
var initY="";
var bDrag = true;
var bDoReal = true;
function fillDiv(div,basicStr,count
{
var div=document.getElementByI
var str="";
for (var i=0;i<counter;i++)
{
str+=basicStr
str+=(i%10==0)?" ":"";
}
div.innerHTML=str;
}
function scrollDiv()
{
var div=document.getElementByI
div.scrollTop=(initY-event
div.scrollLeft=(initX-even
}
function dragDiv(doEnd)
{
var div=document.getElementByI
if (!doEnd)
{
initX=event.x+div.scrollLe
initY=event.y+div.scrollTo
div.onmousemove=scrollDiv
event.srcElement.style.cur
}
else
{
div.onmousemove=null
event.srcElement.style.cur
}
}
function actionDiv(doEnd)
{
if (bDrag)
{
dragDiv(doEnd)
}
else
{
drawBox(doEnd);
}
}
var oBox = null;
function drawBox(doEnd)
{
var div=document.getElementByI
if (!doEnd)
{
if (!oBox)
{
oBox=document.createElemen
document.body.appendChild(
div.onmousemove=drawBox
with(oBox.style)
{
position="absolute";
border="2px dashed";
width = "20px";
height = "15px";
}
}
else //Case that mouse is now moving, drag the size
{
with(oBox.style)
{
left = event.x-2
top = event.y-2
}
}
}
else if (oBox)
{
//Box is up, calculate the center of the box and zoom into it.
var oImage = div.firstChild;
var iHeight = parseInt(oBox.offsetHeight
var iWidth = parseInt(oBox.offsetWidth)
var iPosXImage = posX(oImage)
var iPosYImage = posY(oImage)
var iPosEvent_X = event.clientX;
var iPosEvent_Y = event.clientY;
var iDistanceInImage_X = event.screenX- iPosXImage;
var iDistanceInImage_Y = event.screenY- iPosYImage;
var iCenter_X = Math.round( iDistanceInImage_X -( iWidth));
var iCenter_Y = Math.round(iDistanceInImag
var iZoomFactor = Math.floor((oImage.style.z
if (iZoomFactor >400)
{
iZoomFactor = 400
}
//Calculate offset of (iScrollTo_X/iScrollTo_Y) from center of Image;
//Center of Image
var iCenterOfImage_X = iPosXImage + (oImage.parentNode.offsetW
var iCenterOfImage_Y = iPosYImage +(oImage.parentNode.offset
//distance from posX of Event
var iDistance_X = iPosEvent_X - iCenterOfImage_X;
var iDistance_Y = iPosEvent_Y - iCenterOfImage_Y;
//Scroll To Point :
var iRelativeScrollTop = Math.round( parseInt(div.scrollTop) ) /// (iZoomFactor/100) );
var iRelativeScrollLeft = Math.round( parseInt(div.scrollLeft) ) /// (iZoomFactor/100) );
var iScrollTo_X = iRelativeScrollLeft + (iDistance_X * -1 ) ;
var iScrollTo_Y = iRelativeScrollTop + (iDistance_Y* -1) ;
//var iScrollTo_X = (iCenter_X + iRelativeScrollLeft + iDistance_X) //* (iZoomFactor/100);
//var iScrollTo_Y = (iCenter_Y + iRelativeScrollTop + iDistance_Y) // * (iZoomFactor/100) ;
//var iScrollTo_Y = (event.y + iRelativeScrollTop + iDistance_Y + iCenter_Y) - (div.offsetHeight/2)
//var iScrollTo_X = (event.x + iRelativeScrollLeft + iDistance_X + iCenter_X) - (div.offsetWidth/2)
var oTA = document.getElementById("o
oTA.innerText+="\n" +iIndex + ": [X:" + iScrollTo_X+",Y:" + iScrollTo_Y+"]\n"+iIndex + ": [xdis" + iDistance_X + ",ydis" +iDistance_Y+"]";
xPoint.value = xScroll.value = iScrollTo_X;
yPoint.value = yScroll.value = iScrollTo_Y;
DrawPoint(div, iScrollTo_X, iScrollTo_Y);
if (bDoReal)
{
ScrollTo(div, iScrollTo_X, iScrollTo_Y);
oImage.style.zoom = iZoomFactor + "%";
}
else
{
document.getElementById("x
document.getElementById("y
}
//debugger;
oBox.parentNode.removeChil
div.onmousemove=null
oBox = null;
event.srcElement.style.cur
}
}
function ScrollTo(oParent, x, y )
{
oParent.scrollTop = x;
oParent.scrollLeft = y;
}
//Debug
var iIndex = 0;
function DrawPoint(oParent, x, y )
{
var oPoint = document.createElement("DI
oPoint.innerText = iIndex;
with (oPoint.style)
{
fontSize="9px";
zIndex=999
overflow="hidden";
backgroundColor = "yellow";
height=oPoint.style.width=
position="absolute";
left= x ;
top= y;
}
oParent.appendChild(oPoint
++ iIndex;
}
//Debug
function changeDoReal()
{
bDoReal = !bDoReal;
document.getElementById("D
}
function changeZoomState(oButton)
{
bDrag = !bDrag;
oButton.innerText = (bDrag) ?"Drag" : "Zoom";
}
function posX(obj)
{
var curleft = 0;
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
return curleft;
}
function posY(obj)
{
var curtop = 0;
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
return curtop;
}
-->
</SCRIPT>
</HEAD>
<BODY>
<button onclick ="changeDoReal()" >Do Real</button>[<span id="DoReal">true</span>]<B
<button onclick="DrawPoint(documen
X <input type="text" style="width:30px" id="xPoint" value="10">, Y <input type="text" style="width:30px" id="yPoint" value="15"><br>
<button onclick="ScrollTo(document
X <input type="text" style="width:30px" id="xScroll" value="10">, Y <input type="text" style="width:30px" id="yScroll" value="15"><br>
Should Be: <br>
X[<span id="xShould"></span>], Y [<span id="yShould"></span>]<br>
<button onclick="changeZoomState(t
<button onclick="document.getEleme
<textarea cols="26" rows="27" id="output"></textarea>
<DIV style="position:absolute;w
<img src="http://www.templetons
</DIV><br>
</BODY>
</HTML>
--------------------------
Good luck and thanks for the effort.
Regards,
Avner.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The ScrollTo function is swapping x and y coordinates.
Change it to this:
function ScrollTo(oParent, x, y ) {
oParent.scrollLeft = x;
oParent.scrollTop = y;
}
Also reduce your zooming function to this:
var oBox = null;
function drawBox(doEnd) {
var div=document.getElementByI
if (!doEnd) {
if (!oBox) {
oBox=document.createElemen
//document.body.appendChil
div.appendChild(oBox);
oBox.shiftX = event.x+div.scrollLeft;
oBox.shiftY = event.y+div.scrollTop;
div.onmousemove=drawBox
with(oBox.style) {
position="absolute";
border="2px dashed";
width = "20px";
height = "15px";
}
} else {//Case that mouse is now moving, drag the size
with(oBox.style) {
left = event.x-div.offsetLeft+div
top = event.y-div.offsetTop+div.
}
}
} else if (oBox) {
//Box is up, calculate the center of the box and zoom into it.
var oImage = div.firstChild;
var divH = parseInt(div.offsetHeight)
var divW = parseInt(div.offsetWidth);
var boxH = parseInt(oBox.offsetHeight
var boxW = parseInt(oBox.offsetWidth)
var boxX = oBox.offsetLeft;
var boxY = oBox.offsetTop;
var imgX = oImage.offsetLeft;
var imgY = oImage.offsetTop;
var boxCenter_X = Math.round(boxX + (boxW/2));
var boxCenter_Y = Math.round(boxY + (boxH/2));
var oZoomFactor = Math.floor((oImage.style.z
var nZoomFactor = Math.floor(oZoomFactor * 1.2);
if (nZoomFactor > 400) {
nZoomFactor = 400
}
//Scroll To Point :
var newBoxCenterX = Math.round(boxCenter_X / oZoomFactor * nZoomFactor)
var newBoxCenterY = Math.round(boxCenter_Y / oZoomFactor * nZoomFactor)
var divScrollTo_X = Math.round(newBoxCenterX - (divW / 2));
var divScrollTo_Y = Math.round(newBoxCenterY - (divH / 2));
if(divScrollTo_X<0) divScrollTo_X = 0;
if(divScrollTo_Y<0) divScrollTo_Y = 0;
var theForm = document.forms[0];
var oTA = theForm.output;
oTA.value +="\n" +iIndex + ": [X:" + divScrollTo_X+",Y:" + divScrollTo_Y+"]\n"+iIndex
theForm.xPoint.value = theForm.xScroll.value = divScrollTo_X;
theForm.yPoint.value = theForm.yScroll.value = divScrollTo_Y;
DrawPoint(div, newBoxCenterX, newBoxCenterY);
if (bDoReal) {
ScrollTo(div, divScrollTo_X, divScrollTo_Y);
oImage.style.zoom = nZoomFactor + "%";
} else {
document.getElementById("x
document.getElementById("y
}
//debugger;
oBox.parentNode.removeChil
div.onmousemove=null
oBox = null;
event.srcElement.style.cur
}
}
Add <form> and </form> inside of your <body> section and replace innerText by innerHTML.
Good luck,
ZeroPage
Uhps, the oBox initialization missed the x and y coordinates.
So here the revised function:
var oBox = null;
function drawBox(doEnd) {
var div=document.getElementByI
if (!doEnd) {
if (!oBox) {
oBox=document.createElemen
//document.body.appendChil
div.appendChild(oBox);
div.onmousemove=drawBox
with(oBox.style) {
position="absolute";
border="2px dashed";
width = "20px";
height = "15px";
left = event.x-div.offsetLeft+div
top = event.y-div.offsetTop+div.
}
} else {//Case that mouse is now moving, drag the size
with(oBox.style) {
left = event.x-div.offsetLeft+div
top = event.y-div.offsetTop+div.
}
}
} else if (oBox) {
//Box is up, calculate the center of the box and zoom into it.
var oImage = div.firstChild;
var divH = parseInt(div.offsetHeight)
var divW = parseInt(div.offsetWidth);
var boxH = parseInt(oBox.offsetHeight
var boxW = parseInt(oBox.offsetWidth)
var boxX = oBox.offsetLeft;
var boxY = oBox.offsetTop;
var imgX = oImage.offsetLeft;
var imgY = oImage.offsetTop;
var boxCenter_X = Math.round(boxX + (boxW/2));
var boxCenter_Y = Math.round(boxY + (boxH/2));
var oZoomFactor = Math.floor((oImage.style.z
var nZoomFactor = Math.floor(oZoomFactor * 1.2);
if (nZoomFactor > 400) {
nZoomFactor = 400
}
//Scroll To Point :
var newBoxCenterX = Math.round(boxCenter_X / oZoomFactor * nZoomFactor)
var newBoxCenterY = Math.round(boxCenter_Y / oZoomFactor * nZoomFactor)
var divScrollTo_X = Math.round(newBoxCenterX - (divW / 2));
var divScrollTo_Y = Math.round(newBoxCenterY - (divH / 2));
if(divScrollTo_X<0) divScrollTo_X = 0;
if(divScrollTo_Y<0) divScrollTo_Y = 0;
var theForm = document.forms[0];
var oTA = theForm.output;
oTA.value +="\n" +iIndex + ": [X:" + divScrollTo_X+",Y:" + divScrollTo_Y+"]\n"+iIndex
theForm.xPoint.value = theForm.xScroll.value = divScrollTo_X;
theForm.yPoint.value = theForm.yScroll.value = divScrollTo_Y;
DrawPoint(div, newBoxCenterX, newBoxCenterY);
if (bDoReal) {
ScrollTo(div, divScrollTo_X, divScrollTo_Y);
oImage.style.zoom = nZoomFactor + "%";
} else {
document.getElementById("x
document.getElementById("y
}
//debugger;
oBox.parentNode.removeChil
div.onmousemove=null
oBox = null;
event.srcElement.style.cur
}
}
ZeroPage,
I removed some debugging parts from my code, here is your full version, and it still does not work :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Blank:Document </TITLE>
<META NAME="Author" CONTENT="(C)Avner Cohen">
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
var initX="";
var initY="";
var bDrag = true;
var bDoReal = true;
function scrollDiv()
{
var div=document.getElementByI
div.scrollTop=(initY-event
div.scrollLeft=(initX-even
}
function dragDiv(doEnd)
{
var div=document.getElementByI
if (!doEnd)
{
initX=event.x+div.scrollLe
initY=event.y+div.scrollTo
div.onmousemove=scrollDiv
event.srcElement.style.cur
}
else
{
div.onmousemove=null
event.srcElement.style.cur
}
}
function actionDiv(doEnd)
{
if (bDrag)
{
dragDiv(doEnd)
}
else
{
drawBox(doEnd);
}
}
var oBox = null;
function drawBox(doEnd) {
var div=document.getElementByI
if (!doEnd) {
if (!oBox) {
oBox=document.createElemen
//document.body.appendChil
div.appendChild(oBox);
div.onmousemove=drawBox
with(oBox.style) {
position="absolute";
border="2px dashed";
width = "20px";
height = "15px";
left = event.x-div.offsetLeft+div
top = event.y-div.offsetTop+div.
}
} else {//Case that mouse is now moving, drag the size
with(oBox.style) {
left = event.x-div.offsetLeft+div
top = event.y-div.offsetTop+div.
}
}
} else if (oBox) {
//Box is up, calculate the center of the box and zoom into it.
var oImage = div.firstChild;
var divH = parseInt(div.offsetHeight)
var divW = parseInt(div.offsetWidth);
var boxH = parseInt(oBox.offsetHeight
var boxW = parseInt(oBox.offsetWidth)
var boxX = oBox.offsetLeft;
var boxY = oBox.offsetTop;
var imgX = oImage.offsetLeft;
var imgY = oImage.offsetTop;
var boxCenter_X = Math.round(boxX + (boxW/2));
var boxCenter_Y = Math.round(boxY + (boxH/2));
var oZoomFactor = Math.floor((oImage.style.z
var nZoomFactor = Math.floor(oZoomFactor * 1.2);
if (nZoomFactor > 400) {
nZoomFactor = 400
}
//Scroll To Point :
var newBoxCenterX = Math.round(boxCenter_X / oZoomFactor * nZoomFactor)
var newBoxCenterY = Math.round(boxCenter_Y / oZoomFactor * nZoomFactor)
var divScrollTo_X = Math.round(newBoxCenterX - (divW / 2));
var divScrollTo_Y = Math.round(newBoxCenterY - (divH / 2));
if(divScrollTo_X<0) divScrollTo_X = 0;
if(divScrollTo_Y<0) divScrollTo_Y = 0;
var theForm = document.forms[0];
DrawPoint(div, newBoxCenterX, newBoxCenterY);
if (bDoReal) {
ScrollTo(div, divScrollTo_X, divScrollTo_Y);
oImage.style.zoom = nZoomFactor + "%";
} else {
}
//debugger;
oBox.parentNode.removeChil
div.onmousemove=null
oBox = null;
event.srcElement.style.cur
}
}
function ScrollTo(oParent, x, y )
{
oParent.scrollTop = x;
oParent.scrollLeft = y;
}
//Debug
var iIndex = 0;
function DrawPoint(oParent, x, y )
{
var oPoint = document.createElement("DI
oPoint.innerText = iIndex;
with (oPoint.style)
{
fontSize="9px";
zIndex=999
overflow="hidden";
backgroundColor = "yellow";
height=oPoint.style.width=
position="absolute";
left= x ;
top= y;
}
oParent.appendChild(oPoint
++ iIndex;
}
//Debug
function changeDoReal()
{
bDoReal = !bDoReal;
document.getElementById("D
}
function changeZoomState(oButton)
{
bDrag = !bDrag;
oButton.innerText = (bDrag) ?"Drag" : "Zoom";
}
function posX(obj)
{
var curleft = 0;
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
return curleft;
}
function posY(obj)
{
var curtop = 0;
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
return curtop;
}
-->
</SCRIPT>
</HEAD>
<BODY>
<button onclick="changeZoomState(t
<button onclick="document.getEleme
<DIV style="position:absolute;w
<img src1="http://www.templeton
</DIV><br>
</BODY>
</HTML>
Business Accounts
Answer for Membership
by: PurplePerlsPosted on 2003-12-23 at 14:03:09ID: 9994003
Hello,
d("moshe") ; t("DIV"); d(oBox); .scrollLef t; scrollTop;
I do not know if that helps you, but I would change the relationship of your small box to this:
function drawBox(doEnd)
{
var div=document.getElementByI
if (!doEnd)
{
if (!oBox)
{
oBox=document.createElemen
//document.body.appendChil
div.appendChild(oBox);
div.onmousemove=drawBox
with(oBox.style)
{
position="absolute";
border="2px dashed";
width = "20px";
height = "15px";
}
}
else //Case that mouse is now moving, drag the size
{
with(oBox.style)
{
left = event.x-div.offsetLeft+div
top = event.y-div.offsetTop+div.
}
}
}...