这个写的就靠谱了吧~~嘻嘻~~
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>联动菜单22</title> <style type="text/css"> *{ margin:0; padding:0;} li{ list-style:none;} .nav-wrapper{ width:100%; position:fixed; top:0;} .nav{ width:960px; height:30px; line-height:30px; margin:0 auto;} .nav li{ float:left; width:100px; text-align:center; background:#F2F2F2; margin-right:5px;} .nav li.selected{ color:#fff; background:red;} .box{ width:960px; height:200px; background:#CCC; margin:10px auto 0;} </style> <script type="text/javascript"> window.onload=function(){ var lis=document.getElementById('nav').getElementsByTagName('li'); var divs=document.getElementById('boxWrapper').getElementsByTagName('div'); var i=0; window.onscroll=function(){ LinkageMenu(divs,lis,'100','selected') } }; function LinkageMenu(element,nav,value,className){ var currentIndex=0; for(i=0; i<element.length; i++){ var elementTop=element[i].getBoundingClientRect().top; if(elementTop<value){ currentIndex=i; } } for(i=0; i<nav.length; i++){ nav[i].className=''; } nav[currentIndex].className=className; } </script> </head> <body style="height:3000px;"> <div class="nav-wrapper"> <div class="nav" id="nav"> <ul> <li>菜单一</li> <li>菜单二</li> <li>菜单三</li> <li>菜单四</li> <li>菜单五</li> </ul> </div> </div> <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> <div id="boxWrapper"> <div class="box"> <h1>菜单一</h1> </div> <div class="box"> <h1>菜单二</h1> </div> <div class="box"> <h1>菜单三</h1> </div> <div class="box"> <h1>菜单四</h1> </div> <div class="box"> <h1>菜单五</h1> </div> </div> </body> </html>