2

I'm building a dropdown menu for a project I'm working on, and I've come across some trouble. It is built so that the width and the style.left of each submenu is set by a JavaScript function that is called when the root-level menu items are hovered. When I hover these menu items it looks like this:

The submenu is clearly off by quite some pixels to the left. If I don't alter the style.left of the submenu I get the following instead:

Here the alignment is correct. The fault has occured in both Mozilla Firefox and Google Chrome for both Windows 7 and Linux, so it's not a platform related fault.

Here is the code that produces the error:

menu.js

function show_sub_menu(cath){
    var menu_item = document.getElementById(cath)    //cath is an integer passed to the function
    var m_width = Math.floor(window.innerWidth*0.7*0.2);  //Menu is 70% of window, each item is 20% of menu
    menu_item.style.left = cath*m_width;  //Set the style.left dynamically depending on what menu item is to be displayed
                                          //This last line of code produces the error
}

menu.css

#m_wrapper{
    position:relative;
    width:100%;
}

#menu{
    position:relative;
    width:70%;
}

#menu li{
    width:20%;
    float:left;
}

#menu div{
    position:absolute;
    width:20%;
    top:30px;
}

#menu div a{
    position:relative;
    display:block;
    padding:5px;
}

menu.htm

<div id=m_wrapper>
    <ul id=menu>
        <li onMouseOver=show_sub_menu('0')>Item 1</li>
            <div id=0 onMouseOver=show_sub_menu('0')>
                <a href=#>Item 1.1</a>
                <a href=#>Item 1.2</a>
            </div>
        </li>
    </ul>
</div>

I this seems very illogical, since m_width in the .js is, for my screen settings, 235px and m_width*cath is 0. I am fairly new with JavaScript, so help would be very appreciated!

EscalatedQuickly
  • 400
  • 4
  • 22
  • 2
    I don't mean to be a pain, but would you be able to reproduce this issue in a jsfiddle? – What have you tried Apr 17 '13 at 20:00
  • 2
    Your HTML seems to have an extra ``. I assume you want the `
    ` inside the `
  • `.
– showdev Apr 17 '13 at 20:01
  • 2
    As a side note, don't start your ID with a number – Huangism Apr 17 '13 at 20:11
  • @Evan: I am working on it, will post link as soon as I got it up and running! – EscalatedQuickly Apr 17 '13 at 21:02
  • @showdev: No, it is the second that is too much, but thanks for noticing! – EscalatedQuickly Apr 17 '13 at 21:04
  • @Huangism: Is there something that could be affected by that? I've never heard this before.. – EscalatedQuickly Apr 17 '13 at 21:04
  • @Psyberion Yes, it is against HTML4 spec to use numeric IDs. Good catch, Huangism. http://stackoverflow.com/questions/7987636/why-cant-i-have-a-numeric-value-as-the-id-of-an-element – showdev Apr 17 '13 at 21:06
  • @showdev: I did not know this, thanks for enlightening me! I will have to find a work-around for that then. – EscalatedQuickly Apr 17 '13 at 21:12