Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Absolute Left and Top of Element 1

Status
Not open for further replies.

maxhugen

Programmer
May 25, 2004
498
AU
I'm trying to find the absolute Left and Top of an Element so that I can display a Tooltip, but so far I'm just getting relative values. I'm trying:

Code:
function showtip(current,e,num)	{
	if (document.layers) // Netscape 4.0+ {
		theString="<DIV CLASS='ttip'>"+tip[num]+"</DIV>"
		document.tooltip.document.write(theString)
		document.tooltip.document.close()
		document.tooltip.left=e.pageX+14+'px'
		document.tooltip.top=e.pageY+2+'px'
		document.tooltip.visibility="show"
	} else {
		if(document.getElementById) // Netscape 6.0+ and Internet Explorer 5.0+ {
			elm=document.getElementById("tooltip")
			elml=current
			elm.innerHTML=tip[num]
            elm.style.left=parseInt(elml.left+elml.width+14)+'px'
			elm.style.top=parseInt(elml.top+2)+'px'
			elm.style.visibility = "visible"
		}
	}
}

I'm struggling to find a Javascript method to do this... can anyone point me in the right direction pls?

Max Hugen
Australia
 
If you were using the Prototype JS framework, it'd be a great one-liner:

Code:
var elPos = Element.cumulativeOffset(el);
alert(elPos.left);
alert(elPos.top);

But since you might not be using that, here's how they do it (paraphrased):

Code:
function cumulativeOffset(element) {
	var valueT = 0, valueL = 0;
	do {
		valueT += element.offsetTop  || 0;
		valueL += element.offsetLeft || 0;
		element = element.offsetParent;
	} while (element);
	return ({left:valueL, top:valueT});
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top