首先需要对监听的组件添加相对定位
一般我们监听的都是body
,所以直接给body一个相对定位
body {
position: relative;
}
给监听的组件添加
position: relative;
之后,通过 js代码启动滚动监听插件
$('body').scrollspy({ target: '链接标签的id/类名' })
实例:?
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
position: relative;
padding-top: 50px;
}
#box1 {
width: 500px;
height: 500px;
}
</style>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#box">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="box">?链接标签
<ul class="nav navbar-nav">
<li><a href="#gd1">滚动1</a></li>
<li><a href="#gd2">滚动2</a></li>
<li><a href="#gd3">滚动3</a></li>
<li><a href="#gd4">滚动4</a></li>
<li><a href="#gd5">滚动5</a></li>
</ul>
</div>
</div>
</div>
<div id="box1">
<h4 id="gd1">滚动1</h4>
<p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui.
Tumblr
farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3
wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of
them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony
biodiesel keffiyeh artisan ullamco consequat.</p>
<h4 id="gd2">滚动2</h4>
<p>Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua cupidatat
mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater food truck,
mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic exercitation quis
gentrify. Brooklyn adipisicing craft beer vice keytar deserunt.</p>
<h4 id="gd3">滚动3</h4>
<p>Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing banh
mi,
velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl, echo park
consequat
quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim messenger bag. Cred ex
in,
sustainable delectus consectetur fanny pack iphone.</p>
<h4 id="gd4">滚动4</h4>
<p>In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam.
Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers
irure
four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party
fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi
sustainable.
Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn
nesciunt.</p>
<h4 id="gd5">滚动5</h4>
<p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui.
Tumblr
farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3
wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of
them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony
biodiesel keffiyeh artisan ullamco consequat.</p>
<p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda.
Locavore
sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie gluten-free
lo-fi
fap aliquip. Labore elit placeat before they sold out, terry richardson proident brunch nesciunt quis cosby
sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan readymade velit. VHS chambray
laboris
tempor veniam. Anim mollit minim commodo ullamco thundercats.
</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
<script>
$('body').scrollspy({target: '#box'});
</script>
</body>
</html>
效果图:?