版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zeping891103/article/details/79064422
滚动监听(Scrollspy)插件,即自动更新导航插件,会根据滚动条的位置自动更新对应的导航目标。其基本的实现是随着您的滚动,基于滚动条的位置向导航栏添加 .active class。
Scrollspy的样式有以下几种:
data-spy="scroll",放在想要监听的元素,一般为<body>或<div>
data-target=".navbar",滚动导航栏,一般为<nav>
data-offset,当计算滚动位置时,距离顶部的偏移像素。
.collapse,滚动的内容栏
.navbar-collapse,滚动的内容栏,表示折叠式导航栏自适应
入参项:
offset,属性data-offset(默认值为10),当计算滚动位置时,距离顶部的偏移像素。
方法:
.scrollspy('refresh'):当通过 JavaScript 调用 scrollspy 方法时,您需要调用 .refresh 方法来更新 DOM。这在 DOM 的任意元素发生变更(即,您添加或移除了某些元素)时非常有用。使用脚本如下:
$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
})
事件:
activate.bs.scrollspy,每当一个新项目被滚动监听激活时,触发该事件。如$('#myScrollspy').on('activate.bs.scrollspy', function () {})
演示代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 滚动监听(Scrollspy)插件</title>
<link rel="stylesheet" href="../libs/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="../libs/jquery/3.2.1/jquery.min.js"></script>
<script src="../libs/bootstrap/3.3.7/bootstrap.min.js"></script>
<style>
body {
position: relative;
}
#section1 {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #1E88E5;
}
#section2 {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #673ab7;
}
#section3 {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #ff9800;
}
#section41 {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #00bcd4;
}
#section42 {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #009688;
}
</style>
</head>
<body data-spy="scroll" data-target=".navbar" data-offset="50">
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar" role="折叠式导航">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li>
<a href="#section1">Section 1</a>
</li>
<li>
<a href="#section2">Section 2</a>
</li>
<li>
<a href="#section3">Section 3</a>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Section 4 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<a href="#section41">菜单子板块1</a>
</li>
<li>
<a href="#section42">菜单子板块2</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</nav>
<div id="section1" class="container-fluid">
<h1>Section 1</h1>
<p>板块内容</p>
</div>
<div id="section2" class="container-fluid">
<h1>Section 2</h1>
<p>板块内容</p>
</div>
<div id="section3" class="container-fluid">
<h1>Section 3</h1>
<p>板块内容</p>
</div>
<div id="section41" class="container-fluid">
<h1>Section 4 菜单子板块1</h1>
<p>板块内容</p>
</div>
<div id="section42" class="container-fluid">
<h1>Section 4 菜单子板块2</h1>
<p>板块内容</p>
</div>
</body>
<script>
$('body').on('activate.bs.scrollspy', function() {
var currentItem = $(".nav li.active > a").text();
console.log(currentItem);
})
</script>
</html>
效果图: