[Javascript] Getting element coordinates.

Chris Tifer christ at saeweb.com
Tue Nov 18 12:12:44 CST 2003


Try this out remembering get a handle on the correct item you want. Mine is
set to for "editImage". This was ripped from a larger set of functions so
it's entirely possible that something is missing for you to test with, but
stepping through the code line-by-line I'm sure you'll be able to understand
what's going on. Watch for wrapping:

================================
var isIE = (navigator.appName.toLowerCase() == "microsoft internet
explorer")
var isNN = (navigator.appName.toLowerCase() == "netscape")

var objItem = document.getElementById("editImage")
var objParent = null
var intX = 0
var intY = 0
do
 { // Walk up our document tree until we find the body
  // and add the distance from the parent to our counter.
  intX += objItem.offsetLeft
  intY += objItem.offsetTop
  objParent = objItem.offsetParent.tagName
  objItem = objItem.offsetParent
 }
while(objParent != 'BODY')


var myScrollX
var myScrollY
(isIE) ? myScrollX = document.body.scrollLeft: myScrollX =
window.pageXOffset;
(isIE) ? myScrollY = document.body.scrollTop: myScrollY =
window.pageYOffset;

var intCRLeft = objCropRegion.offsetLeft - myScrollX
var intCRTop = objCropRegion.offsetTop - myScrollY
var intImgLeft = intX - myScrollX
var intImgTop = intY - myScrollY

alert(intImgLeft + " - " + intImgTop)

================================

Chris Tifer
http://emailajoke.com




----- Original Message ----- 
From: "Hakan M." <hakan at backbase.com>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Tuesday, November 18, 2003 12:13 PM
Subject: [Javascript] Getting element coordinates.


> Hello list.
>
> Problem: I need to determine the _exact_ coordinates of an element on my
> page, relative to the documents top/leftmost corner. It has to work in
> IE5.0+ and Mozilla, both on Windows platform. It has to work on _all_
> elements - static, relative, absolute, float, you name it.
>
> Research suggests:
> Check the discussed browser flaws on ppks page.
> <http://www.quirksmode.org/js/findpos.html>
>
> Solution: None? I'm pondering on a function that iterates up, adding
> borders (if IE5->IE6) and checking for relative positioning and stop if
> I find any (if IE5.5) else continue until you run into...
>
> But it doesn't feel right. Any suggestions, or is it really this bad?
> (Someone please kill all employees at Microsoft?)
>
> Hakan
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list