473,388 Members | 1,400 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,388 software developers and data experts.

CSS and javascript, find absolute position of element

Hello everybody.

This is my first attempt at Javascript, something that is long
overdue. But please bear that in mind, and have mercy (=

My question is simple:

I'm doing a dropdown menu system to work on the <div>'s produced by
our CMS, and every menubutton div has a onMouseOver="openSubmenu(this,
'popup1234')", where popup 1234 is the id of the div with the proper
menu items. (its a bit more complicated, with some timeouts etc.)
All this works fine, and the system is usable, but I want to make it
even better by adding a function to detect if the mouse pointer is
close to the open submenu after its left it.

The idea is to keep the submenu open if the pointer is within 30px in
each direction.

The problem is that I cannot find a way to get the position of the
nested <DIV> that is the submenu, relative to the WHOLE document
(where I capture mouse events)

The div is nested within a series of divs with absolute, float and
relative positions....

Can someone please enlighten me on how to get this position? Help is
greatly appreciated!

Cheers,
.. Knut S.
Jul 23 '05 #1
1 24030

"Knut" <kn**@knutsi.com> wrote in message news:60**************************@posting.google.c om...
Hello everybody.

This is my first attempt at Javascript, something that is long
overdue. But please bear that in mind, and have mercy (=

My question is simple:

I'm doing a dropdown menu system to work on the <div>'s produced by
our CMS, and every menubutton div has a onMouseOver="openSubmenu(this,
'popup1234')", where popup 1234 is the id of the div with the proper
menu items. (its a bit more complicated, with some timeouts etc.)
All this works fine, and the system is usable, but I want to make it
even better by adding a function to detect if the mouse pointer is
close to the open submenu after its left it.

The idea is to keep the submenu open if the pointer is within 30px in
each direction.

The problem is that I cannot find a way to get the position of the
nested <DIV> that is the submenu, relative to the WHOLE document
(where I capture mouse events)

The div is nested within a series of divs with absolute, float and
relative positions....

Can someone please enlighten me on how to get this position? Help is
greatly appreciated!

Cheers,
. Knut S.


I put this in a js file call it in the html.

function getElementPosition(elemID){
var offsetTrail = document.getElementById(elemID);
var offsetLeft = 0;
var offsetTop = 0;
while (offsetTrail){
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
offsetLeft += document.body.leftMargin;
offsetTop += document.body.topMargin;
}
return {left:offsetLeft,top:offsetTop};
}

Courtesy of Danny Goodman

Then:
Called in your html like this:

<div id="popup1234" ...>
<!- -->
</div>

<script type="text/javascript">
alert(getElementPosition('popup1234').top+'|'+getE lementPosition('popup1234').left);
</script>

--
George Hester
__________________________________
Jul 23 '05 #2

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

Similar topics

5
by: Chris Leonard | last post by:
Sorry if I'm posting a similar query again but I think this explanation of what I require is better. I have a page which is very long and as I scroll down the page there are various links, what...
10
by: Stephan Koser | last post by:
Hi, how can I get the absolute position of a relative element? We dynamically create a page with multiple segments which are relatively ordered among each other. In these segments we have...
0
by: chris | last post by:
I'm using the code below to attempt to find out the absolute pixel position of an element inside an activex web browser control (axWebBrowser). This example uses the 'www.google.co.uk' web page...
4
by: Samuel | last post by:
Hi, Is it possible to get absolute position of element with javascript? e.g. I have textbox somewhere in table. Textbox relative to table cell position is 0,0. How can I get Textbox absolute...
4
by: vunet.us | last post by:
I have a DIV element. How can I find mouse position (top and left) inside of this DIV? <div onMouseMove="getPositions();" style="width:200px;height:100px"></div> function getPositions(ev){...
0
by: Microsoft Newsserver | last post by:
Im using IE 6/7 / ASP.NET / Javascript. I need to determine the absolute position of elements on my page client side. In summart, I need to know if an event triggered mouse position appears...
30
by: Bassem | last post by:
Hi, I've two div.s. One contains four images, when click on one, a JS function create an image corresponding to it and append it to the second div. I'm attempting to make each new image is to be...
7
by: chemlight | last post by:
I am having an issue with HTML elements not printing when positioned absolutely when they extend beyond the first page. I am working on some foreign tax refund forms. The forms are cut into multiple...
0
by: shu fenghua | last post by:
The problem is when i want to put an element("a" in my code) into an absolute positioned element and i also want this element("a") has a fixed height, then ie6 will render this element("a") as a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.