1、本代码适用于 bootstrap V3 的 页面滚动监听
2、效果:
3、代码:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>XXXXX</title> <link rel="stylesheet" href="css/bootstrap.min.css" /> <link rel="stylesheet" href="css/m.css" /> <style> ul{padding: 0; margin: 0;} li{list-style: none;} .content-txt{padding:20px; color: #666;} .content-absolute{position: absolute; top: 220px;} .content-txt h2{height: 50px; line-height: 50px; border-bottom: 1px solid #e6e6e6; font-size: 20px; text-align: center; color: #666;} .content-txt p{padding: 5px 0; color: #666; line-height: 24px;} .day-bg{height: 30px;padding: 0 28px 0 18px;background: #ffb90f url(images/sj.gif) right 0 no-repeat; display: inline-block; line-height: 30px;border-radius: 2px 0 0 2px;color: #fff; font-size: 16px;} .cfd dt{font-size: 20px; line-height: 70px;} .cfd dd{line-height: 36px; background-color: #f5f5f5; font-size: 14px; padding: 10px 20px;} .jt-ico,.zs-ico,.yc-ico{background-image: url(images/icon3.png); width: 21px; height: 19px; display: inline-block; vertical-align: middle; margin-right: 10px;} .jt-ico{background-position: 0 0;} .zs-ico{background-position: 0 -34px;} .yc-ico{background-position: 0 -68px;} .title-h3{font-size: 20px;line-height: 40px; color: #343434;} .b-bottom{border-bottom: 15px solid #f7f7f7; padding-bottom: 20px;} .dt-nav{background-color: #fff; padding: 10px; border-bottom: 1px solid #e6e6e6;} .dt-nav li{width: 33.333%; float: left;text-align: center;} .dt-nav li a{margin: 5px; display: block; padding: 10px 0; background-color: #efeff4; border-radius: 5px; text-decoration: none;} .dt-nav .active a,.dt-nav .active a:focus{background-color: #5cacee!important; color: #fff!important;} </style> </head> <body data-spy="scroll" data-target="#navbar-example"> <div class="clearfix header"> <nav id="navbar-example" class="dt-nav" role="navigation"> <ul class="nav clearfix"> <li class="nav"><a href="#scroll1" class="active">产品详情</a></li> <li><a href="#scroll2">行程安排</a></li> <li><a href="#scroll3">费用说明</a></li> <li><a href="#scroll4">注意事项</a></li> <li><a href="#scroll5">购物说明</a></li> <li><a href="#scroll6">预定须知</a></li> </ul> </nav> <div class="content-txt"> <div data-spy="scroll" data-target="#myScrollspy" data-offset="0" style="position: relative;"> <h2 id="scroll1">产品详情</h2> <p class="t2">5月22日,巴东县人民*与中诚信投资集团签订合作框架协议,总投资约40亿元的绿葱坡滑雪及高山运动度假小镇项目正式落地。</p> <h2 id="scroll2">行程安排</h2> <h3 class="day-bg">第一天</h3> <dl class="cfd"> <dt>出发地:武汉-恩施</dt> <dd> <ul> <li><span class="jt-ico"></span>交通: 动车</li> </ul> </dd> </dl> </div> <h2 id="scroll3">费用说明</h2> <h3 class="title-h3">费用包含</h3> <ul> <li>住宿:根据协议,中诚信集团拟在巴东县绿葱坡镇投资建设华中</li> </ul> <h3 class="title-h3 mt20">费用不包含</h3> <ul> <li>住宿:根据协议,中诚信集团拟在巴东县绿葱坡镇投资建设华中</li> </ul> <h2 id="scroll4">注意事项</h2> <p class="t2">5月22日,巴东县人民*与中诚信投资集团签订合作框架协议,总投资约40亿元的绿葱坡滑雪及高山运动度假小镇项目正式落地。</p> <h2 id="scroll5">购物说明</h2> <p class="t2">5月22日,巴东县人民*与中诚信投资集团签订合作框架协议,总投资约40亿元的绿葱坡滑雪及高山运动度假小镇项目正式落地。</p> <h2 id="scroll6">预定须知</h2> <h3 class="title-h3">退改说明</h3> <p class="t2">5月22日,巴东县人民*与中诚信投资集团签订合作框架协议,总投资约40亿元的绿葱坡滑雪及高山运动度假小镇项目正式落地。</p> </div> </div> </body> </html> <script src="js/jquery-2.1.3.min.js"></script> <script src="js/bootstrap.min.js"></script> <script type="text/javascript"> jQuery(document).ready(function() { var f1 = $(window).scrollTop(),f2 = $(".dt-nav").offset().top; $(window).scroll(function() { var q = $(window).scrollTop(); if (q > f2) { $(".dt-nav").addClass("navbar-fixed-top") } else { $(".dt-nav").removeClass("navbar-fixed-top") } }); }); </script>
4、支持 上下滚动,tab自动选中;选中tab,页面自动跳跃到锚点