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!