473,480 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

absolute x,y coordinates of an image?

Hello,
Is it possible to find out the absolute coordinates of the upper
left corner of an image on my html page? I need to use javascript to
overlay something next to it exactly 50 pixels to the right of it.

Much thanks for your help, - Dave

Oct 7 '05 #1
6 9603
Sure:

function measureLeft(oElement){
var iLeft = oElement.offsetLeft;
var oParent = oElement.offsetParent;
while(oParent.nodeName != 'BODY'){
iLeft += oParent.offsetLeft;
oParent = oParent.offsetParent;
}
return iLeft;
}

function measureTop(oElement){
var iTop = oElement.offsetTop;
var oParent = oElement.offsetParent;
while(oParent.nodeName != 'BODY'){
iTop += oParent.offsetTop;
oParent = oParent.offsetParent;
}
return iTop;
}

Oct 7 '05 #2
Note that the code below may fail under various conditions, such as IE
running in strict mode, or if there are scrollable elements such as DIVs
within the page.

See http://www.mattkruse.com/javascript/...sitionTest.php for my
attempt at a more generalized position-finding function. It's not complete
yet, but is more reliable than the method used by most (like the one below).

michael elias wrote:
Sure:

function measureLeft(oElement){
var iLeft = oElement.offsetLeft;
var oParent = oElement.offsetParent;
while(oParent.nodeName != 'BODY'){
iLeft += oParent.offsetLeft;
oParent = oParent.offsetParent;
}
return iLeft;
}

function measureTop(oElement){
var iTop = oElement.offsetTop;
var oParent = oElement.offsetParent;
while(oParent.nodeName != 'BODY'){
iTop += oParent.offsetTop;
oParent = oParent.offsetParent;
}
return iTop;
}


(top-posted for easier reading)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 7 '05 #3
michael elias a écrit :
Sure:

function measureLeft(oElement){
var iLeft = oElement.offsetLeft;
var oParent = oElement.offsetParent;
while(oParent.nodeName != 'BODY'){
iLeft += oParent.offsetLeft;
oParent = oParent.offsetParent;
}
return iLeft;
}

function measureTop(oElement){
var iTop = oElement.offsetTop;
var oParent = oElement.offsetParent;
while(oParent.nodeName != 'BODY'){
iTop += oParent.offsetTop;
oParent = oParent.offsetParent;
}
return iTop;
}


This won't work in MSIE 6 if the image is absolutely positioned.

Gérard
--
remove blah to email me
Oct 8 '05 #4
la***********@zipmail.com wrote:
Hello,
Is it possible to find out the absolute coordinates of the upper
left corner of an image on my html page? I need to use javascript to
overlay something next to it exactly 50 pixels to the right of it.

Much thanks for your help, - Dave


Everything you need is here:

<URL:http://www.quirksmode.org/js/findpos.html>
--
Rob
Oct 8 '05 #5
RobG wrote on 08 okt 2005 in comp.lang.javascript:
Everything you need is here:

<URL:http://www.quirksmode.org/js/findpos.html>


All you need is love?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 8 '05 #6
Sadly, this doesn't work for me on IE 6.0 (Win 2000). IE always
returns 0 when I call findPosX(document.getElementById('navImg'));

Is my HTML formatted incorrectly?

<tr>
<td><img id="navImg" name="navImg" src="images/NAV_07.gif" alt=""
border="0"></td>

<td>

Andt he JS is use is

function calculateLeftCoords() {
leftPos = findPosX(document.getElementById('navImg')) + 45;

Any help is appreciated, - Dave

Oct 8 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10976
by: Ken Kast | last post by:
Here's my situation. I have a statically positioned table that has an image in a cell. I also have some layers, defined by absolute-positioned DIVs for some animation. Everything works until I...
1
1883
by: Siegfried Heintze | last post by:
I want to implement drag and drop for tables, divs, spans. The problem is that I don't know how wide or long my tables, divs and spans are going to be in advance so I cannot use absolute...
15
7027
by: Tony Gahlinger | last post by:
I'm using my browser (Mozilla/5.0 Linux i686 Gecko/20031007 Firebird/0.7) to do some client-side image processing. I want to capture the sequence of coordinates a user clicks on in xxx.jpg in the...
1
4411
by: Asaf Dan | last post by:
Hi, I'm using a panel to show an image. The Image could be bigger or smaller than the panel. I want to show the pixel coordinates of the image on a label next to the panel. I'm using an event...
3
63287
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
0
3453
by: deko | last post by:
I'm trying to implement a custom TreeView that shows a ghost image while dragging. But the form I'm using is different from the sample code found here:...
3
21233
by: jackiepatti | last post by:
QUESTION: I have a web page containing a form that contains an image instead of a submit button, e.g. <form name='myform' action='get' method='otherpage.asp'> <input type='image'...
3
22290
by: Andrzej | last post by:
I have a picturebox on my C# .NET form. The picturebox size mode is set to zoom. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
1
2289
by: Cainnech | last post by:
Hi all, I've got a bit of a challenge. I've got a script which displays the mouse coodinates if you click on an image. Now I would like to convert these coordinates to pixelnumber. Let me see if I...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.