부트스트랩 중국어 매뉴얼 /Bootstrap 滚动监听

Bootstrap 滚动监听

Bootstrap滚动监听(Scrollspy)插件

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

如果您想要单独引用该插件的功能,那么您需要引用scrollspy.js。或者,正如 Bootstrap 插件概览 一章中所提到,您可以引用bootstrap.js或压缩版的bootstrap.min.js

用法

您可以向顶部导航添加滚动监听行为:

  • 通过 data 属性:向您想要监听的元素(通常是 body)添加data-spy="scroll"。然后添加带有 Bootstrap.nav组件的父元素的 ID 或 class 的属性data-target。为了它能正常工作,您必须确保页面主体中有匹配您所要监听链接的 ID 的元素存在。


...

...
  • 通过 JavaScript:您可以通过 JavaScript 调用滚动监听,选取要监听的元素,然后调用.scrollspy()函数:

$('body').scrollspy({ target: '.navbar-example' })

实例

下面的实例演示了通过 data 属性使用滚动监听(Scrollspy)插件:

实例

   Bootstrap 实例 - 滚动监听(Scrollspy)插件    
   

iOS

iOS 是一个由苹果公司开发和发布的手机操作系统。最初是于 2007 年首次发布 iPhone、iPod Touch 和 Apple TV。iOS 派生自 OS X,它们共享 Darwin 基础。OS X 操作系统是用在苹果电脑上,iOS 是苹果的移动版本。

SVN

Apache Subversion,通常缩写为 SVN,是一款开源的版本控制系统软件。Subversion 由 CollabNet 公司在 2000 年创建。但是现在它已经发展为 Apache Software Foundation 的一个项目,因此拥有丰富的开发人员和用户社区。

jMeter

jMeter 是一款开源的测试软件。它是 100% 纯 Java 应用程序,用于负载和性能测试。

EJB

Enterprise Java Beans(EJB)是一个创建高度可扩展性和强大企业级应用程序的开发架构,部署在兼容应用程序服务器(比如 JBOSS、Web Logic 等)的 J2EE 上。

Spring

Spring 框架是一个开源的 Java 平台,为快速开发功能强大的 Java 应用程序提供了完备的基础设施支持。

Spring 框架最初是由 Rod Johnson 编写的,在 2003 年 6 月首次发布于 Apache 2.0 许可证下。


运行实例 »

点击 "运行实例" 按钮查看在线实例

选项

通过 data 属性或 JavaScript 来传递。下表列出了这些选项:

选项名称 类型/默认值 Data 属性名称 描述
offset number
默认值:10
data-offset 当计算滚动位置时,距离顶部的偏移像素。

方法

.scrollspy('refresh'):当通过 JavaScript 调用 scrollspy 方法时,您需要调用.refresh方法来更新 DOM。这在 DOM 的任意元素发生变更(即,您添加或移除了某些元素)时非常有用。下面是使用该方法的语法。

$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
})

实例

下面的实例演示了.scrollspy('refresh')方法的用法:

实例

   Bootstrap 实例 - 滚动监听(Scrollspy)插件方法    
   

iOS × 删除该部分

iOS 是一个由苹果公司开发和发布的手机操作系统。最初是于 2007 年首次发布 iPhone、iPod Touch 和 Apple TV。iOS 派生自 OS X,它们共享 Darwin 基础。OS X 操作系统是用在苹果电脑上,iOS 是苹果的移动版本。

SVN

Apache Subversion,通常缩写为 SVN,是一款开源的版本控制系统软件。Subversion 由 CollabNet 公司在 2000 年创建。但是现在它已经发展为 Apache Software Foundation 的一个项目,因此拥有丰富的开发人员和用户社区。

jMeter × 删除该部分

jMeter 是一款开源的测试软件。它是 100% 纯 Java 应用程序,用于负载和性能测试。

EJB

Enterprise Java Beans(EJB)是一个创建高度可扩展性和强大企业级应用程序的开发架构,部署在兼容应用程序服务器(比如 JBOSS、Web Logic 等)的 J2EE 上。

Spring

Spring 框架是一个开源的 Java 平台,为快速开发功能强大的 Java 应用程序提供了完备的基础设施支持。

Spring 框架最初是由 Rod Johnson 编写的,在 2003 年 6 月首次发布于 Apache 2.0 许可证下。


运行实例 »

点击 "运行实例" 按钮查看在线实例

事件

下表列出了滚动监听中要用到事件。这些事件可在函数中当钩子使用。

事件 描述 实例
activate.bs.scrollspy 每当一个新项目被滚动监听激活时,触发该事件。
$('#myScrollspy').on('activate.bs.scrollspy', function () { // 执行一些动作... })

实例

下面的实例演示了activate.bs.scrollspy事件的用法:

实例

   Bootstrap 实例 - 滚动监听(Scrollspy)插件事件    
   

iOS × 删除该部分

iOS 是一个由苹果公司开发和发布的手机操作系统。最初是于 2007 年首次发布 iPhone、iPod Touch 和 Apple TV。iOS 派生自 OS X,它们共享 Darwin 基础。OS X 操作系统是用在苹果电脑上,iOS 是苹果的移动版本。

SVN

Apache Subversion,通常缩写为 SVN,是一款开源的版本控制系统软件。Subversion 由 CollabNet 公司在 2000 年创建。但是现在它已经发展为 Apache Software Foundation 的一个项目,因此拥有丰富的开发人员和用户社区。

jMeter × 删除该部分

jMeter 是一款开源的测试软件。它是 100% 纯 Java 应用程序,用于负载和性能测试。

EJB

Enterprise Java Beans(EJB)是一个创建高度可扩展性和强大企业级应用程序的开发架构,部署在兼容应用程序服务器(比如 JBOSS、Web Logic 等)的 J2EE 上。

Spring

Spring 框架是一个开源的 Java 平台,为快速开发功能强大的 Java 应用程序提供了完备的基础设施支持。

Spring 框架最初是由 Rod Johnson 编写的,在 2003 年 6 月首次发布于 Apache 2.0 许可证下。


运行实例 »

点击 "运行实例" 按钮查看在线实例


更多实例

创建水平滚动监听

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

实例

   Bootstrap Example       
   

Section 1

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

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

Section 2

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

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

Section 3

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

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

Section 4 Submenu 1

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

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

Section 4 Submenu 2

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

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


运行实例 »

点击 "运行实例" 按钮查看在线实例

如何创建垂直滚动监听

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

实例

   Bootstrap Example       
  

Section 1

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

Section 2

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

Section 3

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

Section 4-1

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

Section 4-2

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


运行实例 »

点击 "运行实例" 按钮查看在线实例