我正在为我正在处理的项目构建一个下拉菜单,但遇到了一些麻烦。它的构建使得每个子菜单的宽度和style.left由一个 JavaScript 函数设置,该函数在悬停根级菜单项时调用。当我悬停这些菜单项时,它看起来像这样:
子菜单在左侧明显偏离了相当多的像素。如果我不改变子菜单的style.left,我会得到以下内容:
这里对齐是正确的。Windows 7 和 Linux 的 Mozilla Firefox 和 Google Chrome 都出现了该故障,因此这不是与平台相关的故障。
这是产生错误的代码:
菜单.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
}
菜单.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;
}
菜单.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>
我这看起来很不合逻辑,因为.js 中的m_width是,对于我的屏幕设置,235px 和m_width*cath是 0。我对 JavaScript 相当陌生,所以非常感谢帮助!