Javascript Menu Positioning - n00b question =)

I am trying to make a javascript drop-down navigation menu for my first
rails web site, and I am having some problems with where the menu list
divs pop up on the page.

What I have is a horizontal menu bar with headings (the headings are
links to the home page of each section), and then lists of the pages
within that same section are supposed to appear directly underneath
their heading when you hover over the heading. Pretty standard stuff.

All of the effects and everything are working great, the only problem I
am having is that the divs that hold the page lists pop up about 50px
farther down and also to the right than I expected, so that they are far
away from the header. This is a really big problem because the menu
disappears when your cursor exits the header, so you never have time to
click on anything in the dropdown menu!

Now, there’s just one more catch:
I ran into a bug with getting my menus to disappear at first, so I found
the solution on another site that makes all of the effects work. But
that code places the div off of the page to the left using CSS, and then
uses JavaScript to bring the div back into view, so… No, I cannot use
CSS to position the div, I have to change the JavaScript.

Ok, so here’s the code:

/* HTML for the headers & menus*/

/JavaScript for hiding/showing menus/

/* Navigation menu style*/ #tabbar { margin: 0px 20px 0px 20px; padding: 0px 0px 0px 0px; text-align: left; display: block; width: 760px; vertical-align: bottom; background-color:#000000; } #tabbarbox { margin: 0 auto; padding: 7px 0 0 0; width: 760px; background: rgb(231,231,231) url("images/tabbar.jpg") no-repeat bottom center; vertical-align: bottom; } .tab, .tabselected { display: table-cell; text-align: center; height: 22px; padding: 0px 2px 0px 2px; margin: 0px 0px 0px 0px; vertical-align: middle; } #tabbar a { display: table-cell; text-align: center; height: 22px; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px; vertical-align: middle; } .tabbar_button_l { display: table-cell; vertical-align: middle; } .tabbar_button_c { margin: 0 7px 0 7px; padding: 2px 0 2px 0; vertical-align: middle; } .tab .tabbar_button_l { background: transparent url(images/tab_unselected_left.jpg) no-repeat top left; } .tab .tabbar_button_c { background: transparent url(images/tab_unselected_central.jpg) repeat-x top; } .tab .tabbar_button_r { background: transparent url(images/tab_unselected_right.jpg) no-repeat top right; } .tabselected .tabbar_button_l { background: transparent url(images/tab_left.jpg) no-repeat top left; } .tabselected .tabbar_button_c { background: transparent url(images/tab_central.jpg) repeat-x top; } .tabselected .tabbar_button_r { background: transparent url(images/tab_right.jpg) no-repeat top right; } .amenu { position: absolute; left: -999em; margin: 25px 0px 0px 2px; padding: 5px 5px 5px 5px; border: 1px solid #A2A2A2; background-color: #E6E6E6; min-width: 88px; text-align: left; } .amenu ul{ padding:0; margin:0px 0px 0px 17px; } .amenu ul li { display: list-item; list-style-type: none; margin: 0; padding: 0; vertical-align: middle; } .amenu ul ul{ padding: 0; margin: 0px 0px 0px 10px; } .amenu:hover{ left: 0em; } /* IE Hack */ .grouphover { left: 0em; }

Sorry, I don’t have a live preview, but hopefully I gave enough info in
this super-long post. =)

I’m sure the answer is not too complicated, as I learned the JavaScript
from online tutorials, and I’m probably just not familiar with the
solution.

Thanks a million in advance for any help guys!

Julia L. wrote:

I am trying to make a javascript drop-down navigation menu for my first
rails web site
[…]

Your issues appear to have nothing to do with Rails, and would probably
be better suited to a JavaScript or CSS forum. Please note, however,
that the JS you posted does not appear (on cursory inspection) to be
terribly good code, and that Rails includes the Prototype JS framework,
which may make your life easier.

If it were me, however, I wouldn’t use JS for dropdown menus. It is
possible to use CSS’s :hover pseudoclass for the same purpose – and it
will work even if JS is turned off. I think this is the preferable
solution in most cases.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Julia L. wrote:

I am trying to make a javascript drop-down navigation menu for my first
rails web site
[…]

Your issues appear to have nothing to do with Rails, and would probably
be better suited to a JavaScript or CSS forum. Please note, however,
that the JS you posted does not appear (on cursory inspection) to be
terribly good code, and that Rails includes the Prototype JS framework,
which may make your life easier.

If it were me, however, I wouldn’t use JS for dropdown menus. It is
possible to use CSS’s :hover pseudoclass for the same purpose – and it
will work even if JS is turned off. I think this is the preferable
solution in most cases.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I started off using :hover, but it did not work in IE, so i had to use
JS as a workaround. As much as I would like to ignore that bug, I’m not
the only person who will be using the website, so unfortunately MS ruins
the pretty code. I also would have much preferred to use CSS, as I
already know how to code it properly, whereas I am just learning
JavaScript.
Anyways, thank you for the reply, I’ll try some JS forums, too.

Julia L. wrote:
[…]

I started off using :hover, but it did not work in IE, so i had to use
JS as a workaround.

Ah, I didn’t realize that :hover on anything but a doesn’t work in IE <
7. Then again, my menu titles are a elements, so this wasn’t an
issue…

By the way, I strongly suggest you take a look at Compass, which
requires you learning Sass (and you might as well throw in Haml while
you’re at it). Haml makes writing templates faster than using HTML and
ERb, Sass abstracts CSS in a very useful way, and Compass runs with the
idea of Sass by providing a framework which, if used properly,
completely does away with the need for presentation-based CSS classes!
I believe it’s essential for good semantic markup.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]