Bootstrap 滚动监听(Scrollspy)

2020-06-19 08:31:31易采站长站整理

Bootstrap 滚动监听(Scrollspy)

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


如何创建滚动监听

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

实例


<!-- The navbar - The elements are used to jump to a section in the scrollable area -->

Section 1

Try to scroll this page and look at the navigation bar while scrolling!

...

实例解析

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

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

注意可滚动项元素上的 id

) 必须匹配导航栏上的链接选项 ()。

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

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

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

实例

Section 1

Try to scroll this page and look at the navigation list while scrolling!

...

相关文章 大家在看