- 引导布局
- Bootstrap - 断点
- Bootstrap - 容器
- Bootstrap - 网格系统
- Bootstrap - 列
- Bootstrap - 排水沟
- Bootstrap - 实用程序
- Bootstrap - Z 索引
- Bootstrap - CSS 网格
- 引导组件
- Bootstrap - 手风琴
- Bootstrap - 警报
- Bootstrap - 徽章
- Bootstrap - 面包屑导航
- Bootstrap - 按钮
- Bootstrap - 按钮组
- Bootstrap - 卡片
- Bootstrap - 轮播
- Bootstrap - 关闭按钮
- Bootstrap - 折叠
- Bootstrap - 下拉菜单
- Bootstrap - 列表组
- Bootstrap - 模态
- Bootstrap - 导航栏
- Bootstrap - 导航和选项卡
- Bootstrap - Offcanvas
- Bootstrap - 分页
- Bootstrap - 占位符
- Bootstrap - 弹出窗口
- Bootstrap - 进展
- Bootstrap-Scrollspy
- Bootstrap - 旋转器
- Bootstrap - 吐司
- Bootstrap - 工具提示
- 引导表单
- Bootstrap - 表单
- Bootstrap - 表单控制
- 引导程序 - 选择
- Bootstrap - 支票和收音机
- Bootstrap - 范围
- Bootstrap - 输入组
- Bootstrap - 浮动标签
- Bootstrap - 布局
- 引导程序 - 验证
- 引导助手
- Bootstrap-Clearfix
- Bootstrap - 颜色和背景
- Bootstrap - 彩色链接
- Bootstrap - 聚焦环
- Bootstrap - 图标链接
- Bootstrap - 位置
- Bootstrap - 比率
- Bootstrap - 堆栈
- Bootstrap - 拉伸链接
- Bootstrap - 文本截断
- Bootstrap - 垂直规则
- Bootstrap - 视觉隐藏
- 引导实用程序
- Bootstrap - 背景
- Bootstrap - 边框
- Bootstrap - 颜色
- Bootstrap - 显示
- Bootstrap-Flex
- Bootstrap - 浮动
- Bootstrap - 交互
- 引导程序 - 链接
- Bootstrap - 对象拟合
- Bootstrap - 不透明度
- Bootstrap - 溢出
- Bootstrap - 位置
- Bootstrap - 阴影
- Bootstrap - 尺寸调整
- Bootstrap - 间距
- Bootstrap - 文本
- Bootstrap - 垂直对齐
- Bootstrap - 可见性
- 引导演示
- Bootstrap - 网格演示
- Bootstrap - 按钮演示
- Bootstrap - 导航演示
- Bootstrap - 博客演示
- Bootstrap - 滑块演示
- Bootstrap - 轮播演示
- Bootstrap - 标头演示
- Bootstrap - 页脚演示
- Bootstrap - 英雄演示
- Bootstrap - 特色演示
- Bootstrap - 侧边栏演示
- Bootstrap - 下拉菜单演示
- Bootstrap - 列表组演示
- Bootstrap - 模态演示
- Bootstrap - 徽章演示
- Bootstrap - 面包屑演示
- Bootstrap - Jumbotrons 演示
- Bootstrap-粘性页脚演示
- Bootstrap-相册演示
- Bootstrap-登录演示
- Bootstrap 定价演示
- Bootstrap-Checkout 演示
- Bootstrap-产品演示
- Bootstrap-封面演示
- Bootstrap-仪表板演示
- Bootstrap-粘性页脚导航栏演示
- Bootstrap-Masonry 演示
- Bootstrap-Starter 模板演示
- Bootstrap-Album RTL 演示
- Bootstrap-Checkout RTL 演示
- Bootstrap-Carousel RTL 演示
- Bootstrap-博客 RTL 演示
- Bootstrap-仪表板 RTL 演示
- Bootstrap 有用资源
- Bootstrap - 问题与解答
- Bootstrap - 快速指南
- Bootstrap - 有用的资源
- Bootstrap - 讨论
Bootstrap-Scrollspy
本章将讨论 Bootstrap 的滚动间谍。当您滚动页面时,Bootstrap scrollspy 会自动定位导航栏内容。
它是如何工作的
当具有锚点href引用的id的元素滚动到视图中时,scrollspy 可以与nav、list group一起使用,也可以与当前页面中的任何锚点元素一起使用。这是它的工作原理。
为了利用scrollspy,您应该有两个东西,即导航、列表组或一组简单的链接,以及可滚动容器,例如<body>或具有特定高度和overflow-y:scroll的自定义元素。
向scrollspy容器添加属性data-bs-spy="scroll"和data-bs-target="#navId",其中“navId”是指相应导航的唯一ID 。如果容器没有任何可聚焦元素,请包含tabindex="0"以保证键盘可访问性。
当您在“sied”容器内滚动时,导航中的锚链接将添加或删除.active类。如果链接的id目标无法解析,它们将被忽略。例如,<a href="#home">home</a>应该有一个相应的<div id="home"></div>。
在不可见元素部分,仅考虑和定位可见元素。
导航栏
滚动到导航栏区域下方以查看活动类别的变化。打开下拉菜单并查看突出显示的项目。
例子
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Scrollspy</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav id="navbarFirstExample" class="navbar bg-body-tertiary px-3 mb-3"> <a class="navbar-brand" href="#">Tutorialspoints</a> <ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link" href="#scrollspyFirstTitle">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspySecondTitle">Services</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyThirdTitle">About us</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyFourthTitle">Contact us</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyFifthTitle">Features</a> </li> </ul> </nav> <div data-bs-spy="scroll" data-bs-target="#navbarFirstExample" data-bs-root-margin="0px 0px -40%" data-bs-smooth-scroll="true" class="scrollspy-example bg-body-tertiary p-3 rounded-2" tabindex="0"> <h4 id="scrollspyFirstTitle">Home</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group.</p> <h4 id="scrollspySecondTitle">Services</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="scrollspyThirdTitle">About us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group.</p> <h4 id="scrollspyFourthTitle">Contact us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="scrollspyFifthTitle">Features</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> </div> </body> </html>
嵌套导航
Scrollspy 支持嵌套.nav并在其父级处于.active时使其处于.active状态。滚动导航栏时查看活动类的变化。
例子
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Scrollspy</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="row mt-2"> <div class="col-4"> <nav id="nestatedNavbar" class="h-100 flex-column align-items-stretch pe-4 border-end"> <nav class="nav nav-pills flex-column"> <a class="nav-link" href="#home">Home</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href="#login">Log in</a> <a class="nav-link ms-3 my-1" href="#logout">Log out</a> </nav> <a class="nav-link" href="#aboutus">About us</a> <a class="nav-link" href="#contactus">Contact Us</a> </nav> </nav> </div> <div class="col-8"> <div data-bs-spy="scroll" data-bs-target="#nestatedNavbar" data-bs-smooth-scroll="true" class="bg-body-tertiary p-3 rounded-2 my-2" tabindex="0"> <div id="home"> <h4>Home</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> </div> <div id="login"> <h5>Log In</h5> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group</p> </div> <div id="logout"> <h5>Log out</h5> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> </div> <div id="aboutus"> <h4>About us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group</p> </div> <div id="contactus"> <h4>Contact us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> </div> </div> </div> </div> </body> </html>
列表组
Scrollspy 支持.list-group。当您滚动到列表组附近时,请查看活动类别的变化。
例子
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Scrollspy</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="row mt-2"> <div class="col-4"> <div id="navbarList" class="list-group my-2"> <a class="list-group-item list-group-item-action" href="#home">Home</a> <a class="list-group-item list-group-item-action" href="#services">Services</a> <a class="list-group-item list-group-item-action" href="#aboutus">About us</a> <a class="list-group-item list-group-item-action" href="#contactus">Contact us</a> <a class="list-group-item list-group-item-action" href="#features">Features</a> </div> </div> <div class="col-8"> <div data-bs-spy="scroll" data-bs-target="#navbarList" data-bs-smooth-scroll="true" class="bg-body-tertiary p-3 rounded-2 my-2" tabindex="0"> <h4 id="home">Home</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area.</p> <h4 id="services">Services</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="aboutus">About us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area.</p> <h4 id="contactus">Contact us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="features">Features</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. </p> </div> </div> </div> </div> </body> </html>
简单的锚点
Scrollspy 适用于所有<a>锚元素,不限于导航元素和列表组。
例子
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Scrollspy</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="row"> <div class="col-4"> <div id="listUsingAnchor" class="text-center"> <nav class="nav nav-pills flex-column mx-3"> <a class="nav-link active" href="#home">Home</a> <a class="nav-link" href="#services">Services</a> <a class="nav-link" href="#aboutus">About us</a> <a class="nav-link" href="#contactus">Contact us</a> <a class="nav-link" href="#features">Features</a> </nav> </div> </div> <div class="col-8"> <div data-bs-spy="scroll" data-bs-target="#listUsingAnchor" data-bs-offset="0" data-bs-smooth-scroll="true" class="bg-body-tertiary p-3 rounded-2 my-2" tabindex="0"> <h4 id="home">Home</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group.</p> <h4 id="services">Services</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="aboutus">About us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="contactus">Contact us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group.</p> <h4 id="features">Features</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group.</p> </div> </div> </div> </body> </html>
不可见元素
不可见元素将被忽略,并且它们相应的导航项将不会被分配 .active类。在不可见包装器中初始化的 Scrollspy 实例会忽略所有目标元素。如果包装器变得可见,请使用刷新方法。这有助于检查可观察的元素。
例子
document.querySelectorAll('#nav-tab>[data-bs-toggle="tab"]').forEach(el => { el.addEventListener('shown.bs.tab', () => { const target = el.getAttribute('data-bs-target') const scrollElem = document.querySelector(`${target} [data-bs-spy="scroll"]`) bootstrap.ScrollSpy.getOrCreateInstance(scrollElem).refresh() }) })
用法
通过数据属性
将data-bs-spy="scroll"添加到要监视的元素(通常是<body>),以快速将滚动监视Behave添加到顶栏导航。然后,使用“data-bs-target”属性指定任何 Bootstrap .nav组件的父元素的 id 或类名称。
例子
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Scrollspy</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body data-bs-spy="scroll" data-bs-target="#navbarDataAttribute"> <div id="navbarDataAttribute" class="my-2"> <ul class="nav nav-pills" role="pillslist"> <li class="nav-item"> <a class="nav-link" href="#scrollspyFirstTitle">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspySecondTitle">Services</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyThirdTitle">About us</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyFourthTitle">Contact us</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyFifthTitle">Features</a> </li> </ul> <div data-bs-spy="scroll" data-bs-target="#navbarDataAttribute" data-bs-root-margin="0px 0px -40%" data-bs-smooth-scroll="true" class="bg-body-tertiary p-3 rounded-2 my-2" tabindex="0"> <h4 id="scrollspyFirstTitle">Home</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group.</p> <h4 id="scrollspySecondTitle">Services</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="scrollspyThirdTitle">About us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="scrollspyFourthTitle">Contact us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group.</p> <h4 id="scrollspyFifthTitle">Features</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> </div> </div> </body> </html>
通过JavaScript
要在顶栏导航上启用滚动监视Behave,请将data-bs-spy="scroll"添加到所需元素(通常是<body>标记)。
在<script>标签内,使用“navbarJavaScript”等标识符或类将滚动间谍应用到组件。
例子
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Scrollspy</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body data-bs-spy="scroll" data-bs-target="#navbarJavaScript"> <div id="navbarJavaScript"> <ul class="nav nav-pills" role="pillslist"> <li class="nav-item"> <a class="nav-link" href="#scrollspyFirstTitle">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspySecondTitle">Services</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyThirdTitle">About us</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyFourthTitle">Contact us</a> </li> <li class="nav-item"> <a class="nav-link" href="#scrollspyFifthTitle">Features</a> </li> </ul> <div data-bs-spy="scroll" data-bs-root-margin="0px 0px -40%" data-bs-smooth-scroll="true" class="scrollspy-example bg-body-tertiary p-3 rounded-2" tabindex="0"> <h4 id="scrollspyFirstTitle">Home</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="scrollspySecondTitle">Services</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="scrollspyThirdTitle">About us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> <h4 id="scrollspyFourthTitle">Contact us</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group. Scrollspy works with nav and list group.</p> <h4 id="scrollspyFifthTitle">Features</h4> <p>Bootstrap Scrollspy targets the navigation bar contents automatically on scrolling the area. Scrollspy works with nav and list group.</p> </div> </div> <script> const scrollSpy = new bootstrap.ScrollSpy(document.body, { target: '#navbarJavaScript' }) </script> </body> </html>
选项
可以通过数据属性或 JavaScript 提供选项
要将选项名称添加到data-bs-,如data-bs-animation={value}中所示,请使用数据属性或 JavaScript。如果使用数据属性,请使用“kebab-case”而不是“camelCase”作为选项名称。例如,使用data-bs-custom-class="beautifier"而不是data-bs-custom-class="beautifier"。
Bootstrap 5.2.0 添加了一个名为data-bs-config属性的新功能,用于将基本组件配置存储为 JSON 字符串。如果一个元素同时具有data-bs-config和单独的 data 属性,则单独的 data 属性的值优先于data-bs-config中的值。甚至,现有的数据属性也可以存储 JSON 值。
data-bs-config、data-bs-和 js 对象组合起来创建最终的配置对象,其中最新的键值会覆盖所有其他对象。
CSS 属性
该表描述了 ScrollSpy 插件的属性及其相应的值。
姓名 | 类型 | 默认 | 描述 |
---|---|---|---|
根边距 |
细绳 | 0 像素 0 像素 -25% |
计算滚动位置时,Intersection Observer rootMargin有效单位。 |
平滑滚动 |
布尔值 | 错误的 |
当用户单击引用 ScrollSpy 可观察量的链接时,启用平滑滚动。 |
目标 |
字符串、DOM 元素 | 无效的 |
指定要应用 Scrollspy 插件的元素。 |
临界点 |
大批 | [0.1,0.5,1] |
路口观察者 计算滚动位置时的阈值有效输入。 |