2011-09-06 86 views
0

嗨我有一个菜单,我想操纵一些JS ...我想菜单是在页面加载时设置点,但是当用户向下滚动页面时,我会就像菜单移动到窗口顶部的固定位置一样。jQuery Scroll/Menu Issue

<div id="div_header"> 
    <img src="images/logo.png" height="72px" class="logo" /> 
</div> 

<div id="mainMenu"> 
<ul> 
    <li> 
     <a class="menuItem">Link</a> 
    </li> 
    <li> 
     <a class="menuItem">Link</a> 
    </li> 
</ul> 

#div_header { 
background:url("../images/top-header.png") repeat-x scroll 0 0 transparent; 
height:80px; 
margin:0 0 10px; 
position:relative; 
z-index:3; 
} 

div#mainMenu { 
position:relative; 
text-align:center; 
top:-16px; 
z-index:2; 
background:url(../images/bg_menu.png) repeat-x; 
width:100%; 
height:41px; 
} 

感谢您的帮助......

回答

0

使用position:fixed;在CSS(但我知道IE6,7它不正常工作)。

否则看一看在this post在计算器

更新

$(window).scroll(function() { 
    if ($(this).scrollTop() > $('#div_header').height()) { 
     $('#mainMenu').css('top', $(this).scrollTop() + "px"); 
    } 
    else { 
     $('#mainMenu').css('top', $('#div_header').height() + "px"); 
    } 
}); 

该代码可能需要一些抛光,但它只是一个想法

+0

是的,我知道的固定财产,但我不能使用它,因为我的菜单不会在正确的位置,在标题前面的菜单顶部会出现一个'洞'... – webwrks

+0

因此,您需要手动处理它'scroll'事件。检查我引用的帖子。我已经更新了示例。 – Samich