Home Web Front-end JS Tutorial Detailed explanation of Bootstrap scroll monitoring (Scrollspy) plug-in_javascript skills

Detailed explanation of Bootstrap scroll monitoring (Scrollspy) plug-in_javascript skills

May 16, 2016 pm 03:03 PM
bootstrap plug-in scrolling listener

Scroll spy (Scrollspy) plug-in, that is, the automatic update navigation plug-in, will automatically update the corresponding navigation target according to the position of the scroll bar. Its basic implementation is to add an .active class to the navigation bar based on the position of the scroll bar as you scroll.

If you want to reference the functionality of this plugin separately, then you need to reference scrollspy.js. Alternatively, as mentioned in the Bootstrap plugin overview chapter, you can reference bootstrap.js or a minified version of bootstrap.min.js.

1. Usage
You can add scroll listening behavior to the top navigation:

1. Through the data attribute: add data-spy="scroll" to the element you want to monitor (usually body). Then add an attribute data-target with the ID or class of the parent element of the Bootstrap .nav component. For this to work, you must ensure that an element exists in the body of the page that matches the ID of the link you want to listen for.

<body data-spy="scroll" data-target=".navbar-example">
 ...
 <div class="navbar-example">
  <ul class="nav nav-tabs">
   ...
  </ul>
 </div>
 ...
</body>
Copy after login

2. Through JavaScript: You can call scroll monitoring through JavaScript, select the element to be monitored, and then call the .scrollspy() function:
$('body').scrollspy({ target: '.navbar-example' })
2. Scroll monitoring
The scroll listening plug-in is used to automatically update navigation items based on the position of the scroll bar and display navigation item highlights.

//基本实例
<nav id="nav" class="navbar navbar-default">
  <a href="#" class="navbar-brand">Web 开发</a>
  <ul class="nav navbar-nav">
    <li>
      <a href="#html5">HTML5</a>
    </li>
    <li>
      <a href="#bootstrap">Bootstrap</a>
    </li>
    <li class="dropdown">
      <a href="#" data-toggle="dropdown">JavaScript <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li>
          <a href="#jquery">jQuery</a>
        </li>
        <li>
          <a href="#yui">Yui</a>
        </li>
        <li>
          <a href="#extjs">Extjs</a>
        </li>
      </ul>
    </li>
  </ul>
</nav>

<div data-offset="0" data-target="#nav" data-spy="scroll" style="height: 200px; overflow: auto; position: relative;padding: 0 10px;">
  <h4 id="html5">HTML5</h4>
  <p>
    标准通用标记语言下的一个应用 HTML 标准自 1999 年 12 月发布的 HTML4.01后,后继的 HTML5 和其它标准被束之高阁,为了推动 Web 标准化运动的发展,一些公司联合起来,成立了一个叫做 Web Hypertext Application Technology Working Group(Web 超文本应用技术工作组 -WHATWG) 的组织。WHATWG 致力于 Web 表单和应用程序,而 W3C(World Wide Web Consortium,万维网联盟) 专注于 XHTML2.0。在 2006 年,

    双方决定进行合作,来创建一个新版本的 HTML。
  </p>
  <h4 id="bootstrap">Bootstrap</h4>
  <p>
    Bootstrap,来自 Twitter,是目前很受欢迎的前端框架。Bootstrap 是基于 HTML、 CSS、 JAVASCRIPT 的, 它简洁灵活, 使得 Web 开发更加快捷。 [1]它由 Twitter的设计师 Mark Otto 和 Jacob Thornton 合作开发,是一个 CSS/HTML 框架。Bootstrap提供了优雅的 HTML 和 CSS 规范,它即是由动态 CSS 语言 Less 写成。Bootstrap 一经推出后颇受欢迎,一直是 GitHub 上的热门开源项目,包括 NASA 的 MSNBC(微软全国广播公司)的 Breaking News 都使用了该项目。[2]国内一些移动开发者较为熟悉的框架,如 WeX5前端开源框架等,也是基于 Bootstrap 源码进行性能优化而来。[3]
  </p>
  <h4 id="jquery">jQuery</h4>
  <p>
    JQuery 是继 prototype 之后又一个优秀的 Javascript 库。 它是轻量级的 js库 ,它兼容 CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+) ,jQuery2.0 及后续版本将不再支持 IE6/7/8 浏览器。jQuery 使用户能更方便地处理 HTML(标准通用标记语言下的一个应用) 、 events、 实现动画效果, 并且方便地为网站提供 AJAX交互。jQuery 还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。 jQuery 能够使用户的 html 页面保持代码和 html 内容分离, 也就是说, 不用再在 html 里面插入一堆 js 来调用命令了, 只需要定义 id 即可。
  </p>
  <h4 id="yui">Yui</h4>
  <p>
    近几年随着 jQuery、Ext 以及 CSS3 的发展,以 Bootstrap 为代表的前端开发框架如雨后春笋般挤入视野, 可谓应接不暇。 不论是桌面浏览器端还是移动端都涌现出很多优秀的框架,极大丰富了开发素材,也方便了大家的开发。这些框架各有特点,本文对这些框架进行初步的介绍与比较, 希望能够为大家选择框架提供一点帮助, 也为后续详细研究这些框架的抛砖引玉。
  </p>
  <h4 id="extjs">Extjs</h4>
  <p>
    ExtJS 可以用来开发 RIA 也即富客户端的 AJAX 应用,是一个用 javascript写的,主要用于创建前端用户界面,是一个与后台技术无关的前端 ajax 框架。因此,可以把 ExtJS 用在.Net、Java、Php 等各种开发语言开发的应用中。ExtJs 最开始基于 YUI 技术,由开发人员 JackSlocum 开发,通过参考 JavaSwing 等机制来组织可视化组件,无论从 UI 界面上 CSS 样式的应用,到数据解析上的异常处理,都可算是一款不可多得的JavaScript 客户端技术的精品。
  </p>
</div>

Copy after login

There are two important attributes here, as shown below:


PS: In a menu and a simple situation, scrolling monitoring highlighting can be stably implemented without data-target setting. But when there are multiple navigations, if you don't associate one of them, it will cause an error, so it is generally necessary to add it.

If you use JavaScript scripting, you can remove data-* and use script attributes to define: offset, spy and target. The specific method is as follows:

//使用脚本方式定义属性

$('#content').scrollspy({
  offset : 0,
  target : '#nav',
}); 

Copy after login

The scroll listener also has an event to switch to a new item.

//The event is bound to the navigation

$('#nav').on('activate.bs.scrollspy', function() {
alert('This event is triggered after the new entry is activated!');
});
The scroll listener also has a method that updates the container DOM.

//HTML part

<section class="sec">
  <h4 id="html5">HTML5 <a href="#" onclick="removeSec(this)">删除此项</a></h4>
  <p>
    ...
  </p>
</section>
Copy after login

//When deleting content, refresh the DOM to avoid navigation monitoring misplacement

function removeSec(e) {
  $(e).parents('.sec').remove();
  $('#content').scrollspy('refresh');
}
Copy after login

Note: This method must use the data-* declaration.

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles