还有另外一个《jQuery高亮楼层导航插件One-Page-Nav – 横向、纵向高亮楼层导航特效》这个主要是随着下拉,会自动高亮导航菜单,可以制作固定侧边导航使用。
今天来分享的是多个导航菜单如何全部跟随固定在顶端的方法(亲测可用)!
方法一,使用jquery实现:
jquery select设置值,jquery 检查,jquery试题有答案的,jquery.steps.js 参数,jquerymobile垂直居中,jquery逗号分隔遍历,jquery 修改top,jquery 按钮点击变色再点击还原,jquery 放大同步缩小,jquery select设置值jquery select设置值,jquery 检查,jquery试题有答案的,jquery.steps.js 参数,jquerymobile垂直居中,jquery逗号分隔遍历,jquery 修改top,jquery 按钮点击变色再点击还原,jquery 放大同步缩小,jquery select设置值
可以实现多个导航菜单在滚动过程中全部跟随固定在浏览器顶端!
代码分享如下:
<!doctypehtml> <html> <head> <metacharset="utf-8"> <title>jquery实现多个导航菜单全部跟随固定在浏览器顶端</title> <scriptsrc="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script> <style> body{ margin:0; padding:0; text-align:center; } .wraper{ width:80%; display:inline-block; border:2pxsolid#CC9; border-top:none; } .toggle{ height:50px; line-height:50px; border-top:2pxsolid#CC9; background:#3FF; } .clear{ width:100%; height:52px; } #div1,#div2,#div3,#div4,#div5{ width:100%; height:500px; background:#666; } .fix-div{ margin:0auto; position:fixed; width:80%; left:0; right:0; z-index:99999; } </style> </head> <body> <divclass="wraper"> <divclass="target"> <divclass="clear"> <divclass="toggle"id="fix1">1</div> </div> <divid="div1"></div> </div> <divclass="target"> <divclass="clear"> <divclass="toggle"id="fix2">2</div> </div> <divid="div2"></div> </div> <divclass="target"> <divclass="clear"> <divclass="toggle"id="fix3">3</div> </div> <divid="div3"></div> </div> <divclass="target"> <divclass="clear"> <divclass="toggle"id="fix4">4</div> </div> <divid="div4"></div> </div> <divclass="target"> <divclass="clear"> <divclass="toggle"id="fix5">5</div> </div> <divid="div5"></div> </div> </div> <script> $.fn.fixedDiv=function(actCls,i){ varthat=$(this), h=that.height(), topVal=(h+2)*i, offsetTop=that.offset().top, scrollTop; functionfix(){ scrollTop=$(document).scrollTop(); console.log("offsetTop:"+offsetTop); console.log("scroll:"+screenTop); if(scrollTop>(offsetTop-topVal)){ that.addClass(actCls); that.css("top",topVal+"px"); }else{ that.removeClass(actCls); } } fix(); $(window).scroll(fix); } $('#fix1').fixedDiv('fix-div',0); $('#fix2').fixedDiv('fix-div',1); $('#fix3').fixedDiv('fix-div',2); $('#fix4').fixedDiv('fix-div',3); $('#fix5').fixedDiv('fix-div',4); </script> </body> </html>
方法二:使用原始JS也可以实现下拉跟随置顶,上拉返回原始位置:
jquery select设置值,jquery 检查,jquery试题有答案的,jquery.steps.js 参数,jquerymobile垂直居中,jquery逗号分隔遍历,jquery 修改top,jquery 按钮点击变色再点击还原,jquery 放大同步缩小,jquery select设置值
<!DOCTYPEhtml> <html> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>test</title> <style> *{ padding:0; margin:0; } #header{ position:fixed; top:0; left:0; height:30px; width:100%; background:#555; opacity:0.8; } #nav,.nav{ height:30px; width:100%; background:#000; opacity:0.8; } .fixed{ position:fixed; top:30px; left:0; } </style> </head> <body> <divid="header"></div> <divstyle="margin-top:30px;"> <scripttype="text/javascript"> //document.write('<divid="nav"></div>'); for(vari=0;i<100;i++) document.write('<div>'+i+'helloworld'+'</div>'); </script> </div> <divid="nav"></div> <scripttype="text/javascript"> for(vari=100;i<200;i++) document.write('<div>'+i+'helloworld'+'</div>'); </script> <scripttype="text/javascript"> functiongetElementViewTop(element){ varactualTop=element.offsetTop; varcurrent=element.offsetParent; while(current!==null){ actualTop+=current.offsetTop; current=current.offsetParent; } varelementScrollTop=0; if(document.compatMode=="BackCompat"){ elementScrollTop=document.body.scrollTop; }else{ elementScrollTop=document.documentElement.scrollTop; } returnactualTop; // returnactualTop-elementScrollTop; } functiongetScroll() { vartop,left,width,height; if(document.documentElement&&document.documentElement.scrollTop){ top=document.documentElement.scrollTop; left=document.documentElement.scrollLeft; width=document.documentElement.scrollWidth; height=document.documentElement.scrollHeight; }elseif(document.body){ top=document.body.scrollTop; left=document.body.scrollLeft; width=document.body.scrollWidth; height=document.body.scrollHeight; } return{'top':top,'left':left,'width':width,'height':height}; } varnav=document.getElementById('nav'); varposTop=getElementViewTop(nav); window.addEventListener('scroll',function(event){ varscrollTop=getScroll().top; varoutput=document.getElementById('output'); output.innerHTML=posTop+''+scrollTop+''+(posTop-scrollTop); if(posTop>=30&&posTop-scrollTop<=30) nav.className='navfixed'; elsenav.className='nav'; },false); </script> <divid="output"style="position:fixed;width:60px;height:60px;right:100px;bottom:100px;background:#DDD">0</div> </body> </html>