943,772 Members | Top Members by Rank

Ad:
Apr 2nd, 2008
0

Positioning, Scrolling & FireFox

Expand Post »
I am sure this is old hat to many of you, but it is new to me.

The idea is to display a floating menu when the mouse is over a non-positioned DIV item.

First, I get the position of the DIV item, which works fine. Then I use style.left and style.top to position the floating menu, which works fine on both IE and FireFox, except when the user scrolls the page on FireFox. When the user scrolls, the style.left and style.top seem to position in the floating menu offset by the scrolled amount.

When I try to correct the effect of FireFox scolling, neither window.pageXOffset or window.scrollX provides the amount scrolled. Both pageXOffset and scrollX are zero.

You can see this working (or not working, really) at http://www.statmatics.com/ltf.

Here are some code details. The floating menu:

html Syntax (Toggle Plain Text)
  1. <style>
  2. #menu {
  3. border:none;
  4. margin: 4px;
  5. position: absolute;
  6. }
  7. </style>
  8.  
  9. <body>
  10. <div id=menu>
  11. </div>
  12. </body>

Get the position of the mouseover item

javascript Syntax (Toggle Plain Text)
  1. var item = document.getElementById (itemId);
  2. var itemWidth = <code to get width of item>
  3. var itemHeight = <code to get height of item>
  4. var itemLeft = 0;
  5. var itemTop = 0;
  6. var offsetTrail = item;
  7. while (offsetTrail) {
  8. itemLeft += offsetTrail.offsetLeft;
  9. itemTop += offsetTrail.offsetTop;
  10. offsetTrail = offsetTrail.offsetParent;

Show the floating menu and position it near the item

javascript Syntax (Toggle Plain Text)
  1. var menu = document.getElementById("menu");
  2. menu.innerHTML = <HTML of the floating menu>
  3. menu.border = "1px solid brown";
  4. var px = (typeof menu.style.left == "string") ? "px" : 0;
  5. menuLeft = Math.floor(itemLeft + 0.7*itemWidth);
  6. menuTop = Math.floor(itemTop - 0.4*itemHeight);
  7. menu.style.left = menuLeft + px;
  8. menu.style.top = menuTop + px;

Any ideas? Thanks for your help.

Paul Richards
Stat-Matics, Inc., Melbourne, Florida
Last edited by peter_budo; Apr 3rd, 2008 at 12:06 pm. Reason: Keep It Organized - please use [code] tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
statmatics is offline Offline
1 posts
since Apr 2008
Apr 8th, 2008
0

Re: Positioning, Scrolling & FireFox

Instead of looking for scroll positions relative to the document, I would read mouse's position to find out where to place the <div>. The event element do the trick, so:

event.clientX is the x position where the event occurred and
event.clientY is the Y position.

Then, the code

mydiv.style.left = (event.clientX + 4) + 'px'; will set left position of "mydiv" 4 pixels right to where the mouse is pointing at.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: JavaScript form validation - please help
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Dropdown menu not working in firefox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC