今天作了一個簡單的功能,頁面往下滾動到必定位置,頂部出現一個浮動的導航欄,點擊導航欄標籤,下面頁面跳轉到相應的區域。回到頂部,導航欄隱藏。javascript
由於頂部有一個浮動的導航欄,因此跳轉到下面頁面的時候,老是蓋住一部分,頂部浮動DIV的高度佔用。html
最簡單的解決辦法就是給錨點加上個下面的樣式:java
anchor{ display: block; position: relative; top: -92px; visibility: hidden; }
示例代碼以下:jquery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> *{margin: 0;padding: 0;} a{color:#000000; text-decoration:none;} .banner{height: 2rem;border: 1px solid #ccc;} .navTab{width:100%;height:1rem;line-height:1rem;font-size:.32rem;background:#fff;border-bottom:5px solid #EEEEEE;display: flex;flex-direction: row;white-space: nowrap;overflow-x: scroll;z-index: 99;} .navTab li{margin-left: 0.3rem;display: inline-block;} .navTab li.active a{padding-bottom:.1rem;color: #12318B;font-weight:bold;border-bottom: 1px solid #12318B;} .fixnav {position: fixed;top: 0; bottom: 0;width: 100%;height:1rem;overflow-x: scroll;overflow-y: hidden;z-index: 99;} .banner{width:7.5rem;height:3.75rem;} .top_title{margin:0.3rem 0.25rem 0;font-size:0.38rem;} .top_text{margin:0.1rem 0.25rem;font-size:0.26rem;line-height:.44rem;display: -webkit-box;-webkit-box-orient: vertical; -webkit-line-clamp: 4;overflow: hidden;} .newslist{margin-top:.3rem;} .col_title{color:#12318B;font-size:.34rem;font-weight:bold;} .anchor{display: block;position: relative;top: -1rem;visibility: hidden;} </style> <title>專題列表</title> </head> <body> <div class="banner"></div> <div class="top_nav fixnav"> <div class="navTab"> <ul> <li class=""><a href="#0010bq008000000">開放日回</a></li> <li class=""><a href="#0010bq007000000">圖文直播</a></li> <li class=""><a href="#0010bq005000000">分析評論</a></li> <li class=""><a href="#0010bq004000000">媒體報道</a></li> </ul> </div> </div> <div class="newslist" style="margin-top:1rem"> <div id="0010bq008000000" class="anchor"></div> <div class="col_title flex-row"><label class="title_Vline"></label>開放日回</div> <div class="TnList" style="height:5rem;">11</div> <div id="0010bq007000000" class="anchor"></div> <div class="col_title flex-row"><label class="title_Vline"></label>圖文直播</div> <div class="TnList" style="height:5rem;">22</div> <div id="0010bq005000000" class="anchor"></div> <div class="col_title flex-row"><label class="title_Vline"></label>分析評論</div> <div class="TnList" style="height:5rem;">33</div> <div id="0010bq004000000" class="anchor"></div> <div class="col_title flex-row"><label class="title_Vline"></label>媒體報道</div> <div class="TnList" style="height:5rem;">44</div> </div> <script src="js/rem.js"></script> <script src='js/jquery1.10.2.min.js'></script> <script src="template/build/template.js'></script> <script > $(document).ready(function() { $(".top_nav").hide(); $(window).scroll(function() { if($(document).scrollTop() >= 200) { $(".top_nav").addClass("fixnav").slideDown(); } else { $(".top_nav").hide(); } }) $(".navTab li").click(function(){ var index = $(this).index(); console.log('點擊了:'+$(this).index()); $(this).addClass('active').siblings().removeClass('active'); }) }); </script> </body> </html>
也能夠js實現頁面滾動,代碼以下:web
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> *{margin: 0;padding: 0;} a{color:#000000; text-decoration:none;} .banner{height: 2rem;border: 1px solid #ccc;} .navTab{width:100%;height:1rem;line-height:1rem;font-size:.32rem;background:#fff;border-bottom:5px solid #EEEEEE;display: flex;flex-direction: row;white-space: nowrap;overflow-x: scroll;z-index: 99;} .navTab li{margin-left: 0.3rem;display: inline-block;} .navTab li.active a{padding-bottom:.1rem;color: #12318B;font-weight:bold;border-bottom: 1px solid #12318B;} .fixnav {position: fixed;top: 0; bottom: 0;width: 100%;height:1rem;overflow-x: scroll;overflow-y: hidden;z-index: 99;} .banner{width:7.5rem;height:3.75rem;} .top_title{margin:0.3rem 0.25rem 0;font-size:0.38rem;} .top_text{margin:0.1rem 0.25rem;font-size:0.26rem;line-height:.44rem;display: -webkit-box;-webkit-box-orient: vertical; -webkit-line-clamp: 4;overflow: hidden;} .newslist{margin-top:.3rem;} .col_title{color:#12318B;font-size:.34rem;font-weight:bold;} .anchor{display: block;position: relative;top: -1rem;visibility: hidden;} .TnList{height:5rem;} </style> <title>專題列表</title> </head> <body> <div class="banner"></div> <div class="top_nav fixnav"> <div class="navTab" id="listColumn"> <ul> <li class="" ><a >開放日回</a></li> <li class="" ><a >圖文直播</a></li> <li class="" ><a >分析評論</a></li> <li class="" ><a >媒體報道</a></li> </ul> </div> </div> <div class="newslist" id="newsList" style="margin-top:1rem"> <!-- <div id="0010bq008000000" class="anchor"></div> <div class="col_title flex-row"><label class="title_Vline"></label>開放日回</div> <div class="TnList" style="height:5rem;">11</div> <div id="0010bq007000000" class="anchor"></div> <div class="col_title flex-row"><label class="title_Vline"></label>圖文直播</div> <div class="TnList" style="height:5rem;">22</div> <div id="0010bq005000000" class="anchor"></div> <div class="col_title flex-row"><label class="title_Vline"></label>分析評論</div> <div class="TnList" style="height:5rem;">33</div> <div id="0010bq004000000" class="anchor"></div> <div class="col_title flex-row"><label class="title_Vline"></label>媒體報道</div> <div class="TnList" style="height:5rem;">44</div> --> </div> <script src="js/rem.js"></script> <script src='js/jquery1.10.2.min.js'></script> <script src="template/build/template.js" type="text/javascript" charset="utf-8"></script> <script src="js/untils.js" type="text/javascript"></script> <script src="js/md5.js" type="text/javascript"></script> <script > $(document).ready(function() { var data = { column:[ {'subjectId':'0010bq008000000', 'subjectTitle':'開放日回'},{'subjectId':'0010bq007000000', 'subjectTitle':'圖文直播'}, {'subjectId':'0010bq005000000', 'subjectTitle':'分析評論'},{'subjectId':'0010bq004000000', 'subjectTitle':'媒體報道'}, ], nlist:[ {'subjectId':'0010bq008000000', 'subjectTitle':'開放日回', articleData:[{'titleLong':'第一條'},{'titleLong':'第二條'},{'titleLong':'第三條'}]}, {'subjectId':'0010bq007000000', 'subjectTitle':'圖文直播', articleData:[{'titleLong':'第一條'},{'titleLong':'第二條'},{'titleLong':'第三條'}]}, {'subjectId':'0010bq005000000', 'subjectTitle':'分析評論', articleData:[{'titleLong':'第一條'},{'titleLong':'第二條'},{'titleLong':'第三條'}]}, {'subjectId':'0010bq004000000', 'subjectTitle':'媒體報道', articleData:[{'titleLong':'第一條'},{'titleLong':'第二條'},{'titleLong':'第三條'}]}, ], }; $(".top_nav").hide(); $(window).scroll(function() { if($(document).scrollTop() >= 200) { $(".top_nav").addClass("fixnav").slideDown(); } else { $(".top_nav").hide(); } }) //Render('listColumn2', data, 'listColumn'); Render('list', data, 'newsList'); $(".navTab").on("click","li",function(){ $(this).addClass('active').siblings().removeClass('active');
var navIndex = $(this).index(); console.log(navIndex); console.log(data.nlist[navIndex].subjectId); var menuTypeId = "div[data-id" + data.nlist[navIndex].subjectId + "]"; console.log('.newslist ' + menuTypeId); $("html, body").animate({ scrollTop: $('.newslist ' + menuTypeId).offset().top}, {duration: 100,easing: "swing" });//scrollTop:指定相關模塊回到指定位置(該位置要去掉菜單導航欄高度)。 }); }); </script> </body> </html>
注意同樣名字的id只能有一個ide