后浪云Bootstrap5教程:Bootstrap5 滚动监听(Scrollspy)

Bootstrap5 滚动监听(Scrollspy)

滚动监听(Scrollspy)插件,即自动更新导航插件,会根据滚动条的位置自动更新对应的导航目标。其基本的实现是随着您的滚动。

如何创建滚动监听

以下实例演示了如何创建滚动监听:

实例

<!--
可滚动区域
-->

<
body

data-bs-spy
=
"
scroll
"

data-bs-target
=
"
.navbar
"

data-bs-offset
=
"
50
"
>

<!--
导航栏 - <a> 元素用于跳到指定滚动区域
-->

<
nav

class
=
"
navbar navbar-expand-sm bg-dark navbar-dark fixed-top
"
>
...
<
ul

class
=
"
navbar-nav
"
>

<
li
>
<
a

href
=
"
#section1
"
>
Section 1
</
a
>
</
li
>
...
</
nav
>

<!--
Section 1
-->

<
div

id
=
"
section1
"
>

<
h1
>
Section 1
</
h1
>

<
p
>
Try to scroll this page and look at the navigation bar while scrolling!
</
p
>

</
div
>
...
</
body
>

尝试一下 »

实例解析

向您想要监听的元素(通常是 body)添加 data-bs-spy="scroll"

然后添加 data-bs-target 属性,它的值为导航栏的 id 或 class (.navbar)。这样就可以联系上可滚动区域。

注意可滚动项元素上的 id (<div id="section1">) 必须匹配导航栏上的链接选项 (<a href="#section1">)。

可选项data-bs-offset 属性用于计算滚动位置时,距离顶部的偏移像素。 默认为 10 px。

设置相对定位: 使用 data-spy="scroll" 的元素需要将其 CSS position 属性设置为 "relative" 才能起作用。

以下实例设置了垂直滚动监听:

实例

<
div

class
=
"
row
"
>

<
div

class
=
"
col-4
"
>

<
nav

id
=
"
navbar-example3
"

class
=
"
navbar navbar-light bg-light flex-column align-items-stretch p-3
"
>

<
a

class
=
"
navbar-brand
"

href
=
"
#
"
>
Navbar
</
a
>

<
nav

class
=
"
nav nav-pills flex-column
"
>

<
a

class
=
"
nav-link
"

href
=
"
#item-1
"
>
Item 1
</
a
>

<
nav

class
=
"
nav nav-pills flex-column
"
>

<
a

class
=
"
nav-link ms-3 my-1
"

href
=
"
#item-1-1
"
>
Item 1-1
</
a
>

<
a

class
=
"
nav-link ms-3 my-1
"

href
=
"
#item-1-2
"
>
Item 1-2
</
a
>

</
nav
>

<
a

class
=
"
nav-link
"

href
=
"
#item-2
"
>
Item 2
</
a
>

<
a

class
=
"
nav-link active
"

href
=
"
#item-3
"
>
Item 3
</
a
>

<
nav

class
=
"
nav nav-pills flex-column
"
>

<
a

class
=
"
nav-link ms-3 my-1
"

href
=
"
#item-3-1
"
>
Item 3-1
</
a
>

<
a

class
=
"
nav-link ms-3 my-1 active
"

href
=
"
#item-3-2
"
>
Item 3-2
</
a
>

</
nav
>

</
nav
>

</
nav
>

</
div
>

<
div

class
=
"
col-8
"
>

<
div

data-bs-spy
=
"
scroll
"

data-bs-target
=
"
#navbar-example3
"

data-bs-offset
=
"
0
"

class
=
"
scrollspy-example-2
"

tabindex
=
"
0
"
>

<
h4

id
=
"
item-1
"
>
Item 1
</
h4
>

<
p
>
...
</
p
>

<
h5

id
=
"
item-1-1
"
>
Item 1-1
</
h5
>

<
p
>
...
</
p
>

<
h5

id
=
"
item-1-2
"
>
Item 1-2
</
h5
>

<
p
>
...
</
p
>

<
h4

id
=
"
item-2
"
>
Item 2
</
h4
>

<
p
>
...
</
p
>

<
h4

id
=
"
item-3
"
>
Item 3
</
h4
>

<
p
>
...
</
p
>

<
h5

id
=
"
item-3-1
"
>
Item 3-1
</
h5
>

<
p
>
...
</
p
>

<
h5

id
=
"
item-3-2
"
>
Item 3-2
</
h5
>

<
p
>
...
</
p
>

</
div
>

</
div
>

</
div
>

尝试一下 »

列表组形式:

实例

<
div

id
=
"
list-example
"

class
=
"
list-group
"
>

<
a

class
=
"
list-group-item list-group-item-action
"

href
=
"
#list-item-1
"
>
Item 1
</
a
>

<
a

class
=
"
list-group-item list-group-item-action
"

href
=
"
#list-item-2
"
>
Item 2
</
a
>

<
a

class
=
"
list-group-item list-group-item-action
"

href
=
"
#list-item-3
"
>
Item 3
</
a
>

<
a

class
=
"
list-group-item list-group-item-action
"

href
=
"
#list-item-4
"
>
Item 4
</
a
>

</
div
>

<
div

data-bs-spy
=
"
scroll
"

data-bs-target
=
"
#list-example
"

data-bs-offset
=
"
0
"

class
=
"
scrollspy-example
"

tabindex
=
"
0
"
>

<
h4

id
=
"
list-item-1
"
>
Item 1
</
h4
>

<
p
>
...
</
p
>

<
h4

id
=
"
list-item-2
"
>
Item 2
</
h4
>

<
p
>
...
</
p
>

<
h4

id
=
"
list-item-3
"
>
Item 3
</
h4
>

<
p
>
...
</
p
>

<
h4

id
=
"
list-item-4
"
>
Item 4
</
h4
>

<
p
>
...
</
p
>

</
div
>

尝试一下 »

THE END