views:

179

answers:

3

Hello!

I've download the Refresh template by Styleshout.com since I really like it. But unfortunately, there was no drop down menu in it, only a normal menu.

So I tried to integrate a drop down menu which I found a nice tutorial for.

It does almost work - almost. Here's the result: the template on my webspace.

The menus are opened - but at the wrong location. Why? What's wrong with my implementation? All 3 drop down lists are opened under the first item.

I hope you can help me. Thanks in advance!

PS: Here's the code:

####################
####### HTML #######
####################
<ul id="nav">
    <li><a href="index.html">Nav #1</a>
        <ul>
            <li><a href="#">Nav #1.1</a></li>
            <li><a href="#">Nav #1.2</a></li>
        </ul>
    </li>
    <li><a href="index.html">Nav #2</a>
        <ul>
            <li><a href="#">Nav #2.1</a></li>
            <li><a href="#">Nav #2.2</a></li>
        </ul>
    </li>
    <li><a href="index.html">Nav #3</a>
        <ul>
            <li><a href="#">Nav #3.1</a></li>
            <li><a href="#">Nav #3.2</a></li>
        </ul>
    </li>
</ul>

####################
#### JAVASCRIPT ####
####################
sfHover = function() {
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onmouseover=function() {
            this.className+=" sfhover";
        }
        sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

###################
####### CSS #######
###################
ul#nav li ul {
    position: absolute;
    left: -9999px;
    top: 38px;
    display: block;
    width: 100px;
    background-color: transparent;
}
ul#nav li {
    position: relative;
}
ul#nav li ul li {
    float: none;
}
/* Links in the drop down lists start */
ul#nav li ul li a {
    clear: left;
    display: block;
    text-decoration: none;
    width: 100px;
    background-color: #333;
}
/* Links in the drop down lists end */
/* Making visible start */
ul#nav li:hover ul, #nav li.sfhover ul {
    left: auto;
}
/* Making visible end */
+1  A: 

You need to float the container LI's left and set the 'top' value on the pop-up boxes to 100%. (Tested only in FF3.5)

ul#nav li {
    position: relative;
    float: left;
}

ul#nav li ul {
    position: absolute;
    left: -9999px;
    top: 100%;
    display: block;
    width: 100px;
    background-color: transparent;
}
medigerati
Thank you very much! At least in Firefox it works now.
A: 

Doesn't really explain it directly, but I would reccomend adding and subtracting a class attribute, not modifying (getAttribute, removeAttribute, etc.). Also, your class name has a space in it ( " sfhover" ) which probably isn't a good idea.

CrazyJugglerDrummer
I don't know why there is a blank space in the class name. I found this JavaScript code in the tutorial. But it works fine in IE.
A: 

Here:

 <li>
    <a>Nav #1</a>
    <ul>
       <li>Nav #1.1</li> 
    <ul>
 </li>

To move the entire menu and its submenu. You will have to set float:left at <li> not <a>

#menu ul li a { 
 float: none;
}
#menu ul li { 
 float: left;
}

One more thing, this doesn't relate to your problem. But, I think you should use display:none/block instead of left:-9999px/auto.

Hope it help.

Bird
My tutorial says: To make the drop down menu work in Opera: "So instead of display: none we use left: -999em to propel the dropdown list out of view and then left: auto (rather than left: 0) to bring it back"
never know that before, thanks.
Bird
If you use "display: none", web accessibility devices such as screenreaders (http://en.wikipedia.org/wiki/Screen_reader) won't read the content. By using "left: -999em", you ensure that screen readers can still see the content, even though it's off the page.
medigerati
Thank you, medigerati, for the explanation.