473,698 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="op enSubmenu(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 24078

"Knut" <kn**@knutsi.co m> wrote in message news:60******** *************** ***@posting.goo gle.com...
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="op enSubmenu(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 getElementPosit ion(elemID){
var offsetTrail = document.getEle mentById(elemID );
var offsetLeft = 0;
var offsetTop = 0;
while (offsetTrail){
offsetLeft += offsetTrail.off setLeft;
offsetTop += offsetTrail.off setTop;
offsetTrail = offsetTrail.off setParent;
}
if (navigator.user Agent.indexOf(' Mac') != -1 && typeof document.body.l eftMargin != 'undefined'){
offsetLeft += document.body.l eftMargin;
offsetTop += document.body.t opMargin;
}
return {left:offsetLef t,top:offsetTop };
}

Courtesy of Danny Goodman

Then:
Called in your html like this:

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

<script type="text/javascript">
alert(getElemen tPosition('popu p1234').top+'|' +getElementPosi tion('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
128478
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 I'd like to be able to do is when I click on a link return the absolute position within the page of that link. I've looked at event.clientY but this just gives me the offset from the top of the current page, suppose I've scrolled down by 3 pages...
10
2364
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 input fields. When such an input field is focused I need it's absolute position. Is there a way to do so with IE > 6? thanks...
0
2999
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 and attempts to find the position of the editbox where the search critera is enetered. My problem I'm having is that the result is always near the element but never exact (in this case, the left co-ordinate seems to be 7 pixels to high).
4
5668
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 position on page?? Please, any ideas, I'm going crazy... thanks
4
26486
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
1147
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 inside a div. Now the rub is that some divs may be absolutely positioned and some may be positioned relatively. I know I can get the position of the mouse using event.clientX or clientY.
30
4884
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 movable. I'm trying for three days with many searches to create a DragPanel and set its TargetControlID to the certian images on the client side. I also tried to set the one only DragPanel's TragetControlID property to each image on focus. Anyone...
7
20486
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 jpg images, so that I can position them on the HTML page, so that they print properly (page1, page2, etc). The information to complete the forms is then positioned using position: absolute and a z-index of +1. Here is an example code of my...
0
1310
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 common block element with a 100% width. I also found that if any css property can make element hasLayout added to the element in absolute positioned element in ie6, the problem will occur. ie7 8 and other standard browser can show it correctly. ...
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
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 we have to send another system
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.