Positioning, Scrolling & FireFox

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Apr 2008
Posts: 1
Reputation: statmatics is an unknown quantity at this point 
Solved Threads: 0
statmatics statmatics is offline Offline
Newbie Poster

Positioning, Scrolling & FireFox

 
0
  #1
Apr 2nd, 2008
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:

  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

  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

  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 24
Reputation: HenryGR is an unknown quantity at this point 
Solved Threads: 4
HenryGR's Avatar
HenryGR HenryGR is offline Offline
Newbie Poster

Re: Positioning, Scrolling & FireFox

 
0
  #2
Apr 8th, 2008
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.
You keep going, have a Nice day!
Henry.

Before printing this message, make sure is necessary.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC