473,378 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

x and y coordinates

How can I find out (using JavaScript) the x and y coordinates of a HTML
element, e.g. an image, an anchor, a div?
Thanks for the help.
Derek

Jul 23 '05 #1
5 10790
Derek wrote:
How can I find out (using JavaScript) the x and y coordinates of a HTML
element, e.g. an image, an anchor, a div?

[...]


Search for "findPosX" posted in the last couple of days.

Have a poke around quirksmode.org:

<URL:http://www.quirksmode.org/js/findpos.html#>
--
Fred
Jul 23 '05 #2
Derek wrote:
How can I find out (using JavaScript) the x and y coordinates of a HTML
element, e.g. an image, an anchor, a div?


This little function should do the trick (credit to jumper on
http://eksperten.dk):

function getPos(elm) {
for(var
zx=zy=0;elm!=null;zx+=elm.offsetLeft,zy+=elm.offse tTop,elm=elm.offsetParent);
return {x:zx,y:zy}
}

To use the function you should use an object (an image, div etc.) as the
argument. The function returns another object with the x and y coordinates:

elm = document.getElementById("divElement");
pos = getPos(elm);
alert(pos.x); // this is the x-coordinate
alert(pos.y); // ..and the y-coordinate
// You can also do something like this:
coordinateX = getPos(elm).x;
// or
coordinateY = getPos(document.getElementById("divElement")).y;
Jul 23 '05 #3
Ulrik Skovenborg wrote:
Derek wrote:
> How can I find out (using JavaScript) the x and y coordinates of a HTML
> element, e.g. an image, an anchor, a div?


This little function should do the trick (credit to jumper on
http://eksperten.dk):

function getPos(elm) {
for(var
zx=zy=0;elm!=null;zx+=elm.offsetLeft,zy+=elm.offse tTop,elm=elm.offsetParent);

return {x:zx,y:zy}
}

To use the function you should use an object (an image, div etc.) as the
argument. The function returns another object with the x and y coordinates:

elm = document.getElementById("divElement");
pos = getPos(elm);
alert(pos.x); // this is the x-coordinate
alert(pos.y); // ..and the y-coordinate
// You can also do something like this:
coordinateX = getPos(elm).x;
// or
coordinateY = getPos(document.getElementById("divElement")).y;


Does that take into consideration absolutely positioned elements when
using CSS?

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 23 '05 #4

Justin Koivisto wrote:

(snip)
Does that take into consideration absolutely positioned elements when using CSS?


Yes. The offset[Left/Top/Width/Height] properties are calculated by the
browser when laying out the page; they're read-only, and available
regardless of whether CSS was even used to position the object. There
are a few browser-specific wrinkles that crop up, but the accumulated
offsets are reasonably accurate. Here's an alternative if you only need
one coordinate (takes id or object ref):

function getLeft(obj)
{
if ('string' == typeof obj)
obj = document.getElementById(obj);
var x = 0;
while (obj != null)
{
x += obj.offsetLeft;
obj = obj.offsetParent;
}
return x;
}

function getTop(obj)
{
if ('string' == typeof obj)
obj = document.getElementById(obj);
var y = 0;
while (obj != null)
{
y += obj.offsetTop;
obj = obj.offsetParent;
}
return y;
}

Jul 23 '05 #5
RobB wrote:
Justin Koivisto wrote:

(snip)

Does that take into consideration absolutely positioned elements when


using CSS?

Yes. The offset[Left/Top/Width/Height] properties are calculated by the
browser when laying out the page; they're read-only, and available
regardless of whether CSS was even used to position the object. There
are a few browser-specific wrinkles that crop up, but the accumulated
offsets are reasonably accurate. Here's an alternative if you only need
one coordinate (takes id or object ref):

function getLeft(obj)
{
if ('string' == typeof obj)
obj = document.getElementById(obj);
var x = 0;
while (obj != null)
{
x += obj.offsetLeft;
obj = obj.offsetParent;
}
return x;
}

function getTop(obj)
{
if ('string' == typeof obj)
obj = document.getElementById(obj);
var y = 0;
while (obj != null)
{
y += obj.offsetTop;
obj = obj.offsetParent;
}
return y;
}


Oops... I was thinking of something slightly different.

I used a (likely inefficient) way to find the position of an object
relative to its absolutely positioned parent (or root if there was no
CSS positioning involved) which worked out to just under 30 lines. I was
hoping to replace it all with that piece. ;)

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 23 '05 #6

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

Similar topics

0
by: Suresh Kumar | last post by:
Hi, In Tkinter, how to convert canvas coordinates to window coordinates? To convert window coordinates to canvas coordinates we are using "canvasx,canvasy". But how to do vice-versa? Iam using a...
0
by: Suresh Kumar | last post by:
Hi, In Tkinter, how to convert canvas coordinates to window coordinates? To convert window coordinates to canvas coordinates we are using "canvasx,canvasy". But how to do vice-versa? With...
2
by: Oldchatterman | last post by:
Hello, in an application I measure a lot of 2d coordinates (x,y) of a pattern. This pattern consists of a set of points on grid with fixed pitches in x and y direction. These coordinates all...
1
by: google | last post by:
Hello, Using ECMA script within an SVG document i'm looking for a way to display an overlay window (rectangle) that follows the mouse cursor an shows some information. It boils down to...
3
by: hclarius | last post by:
Is there any way programmatically to get the values of the PrintDocument's current position (x and y)? Thanks
0
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
by: steve | last post by:
Hi All I have textboxes within a TableLayoutpanel and I want to be able to position an independant control adjacent to a selected textbox This independent control allows selection of text to...
1
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
by: raylopez99 | last post by:
keywords: logical coordinates, page coordinates, world coordinates, device coordinates, physical coordinates, screen coordinates, client coordinates. offset rectangle. WYSIWYG rubber rectangle...
0
by: mingke | last post by:
Hi.... I'm having difficulties with my codes to list coordinates (in my example codes is 4x4 pixel). I tried two different codes and the results are different.Basically, I tried to declare the sub...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.