Bootstrap tutorial JS plug-in rolling listening learning notes sharing, bootstrap learning notes

Source: Internet
Author: User

Bootstrap tutorial JS plug-in rolling listening learning notes sharing, bootstrap learning notes

This article mainly describes the JavaScript plug-in-rolling listener.

1. Case studies

The scroll listening plug-in can automatically update the corresponding navigation tag based on the position of the scroll bar. You can scroll through this page to see the changes in the left navigation bar.

First, you can test the code to see the effect.

<!DOCTYPE html> 

Usage 1 --Data attributes

You can view the code by adding the page elements to be listened to (generally) but adding them to the Div above. Then add the property data-spy = "scroll" to the div to easily add the scroll listening function to the top navigation bar. Then add the data-target attribute to it. The value of this attribute is the ID or class of the parent element of any. nav component in Bootstrap.

Copy codeThe Code is as follows: <div data-offset = "0" class = "scrollspy-example" data-spy = "scroll" data-target = "# navbar-example">
........
</Div>

The navigation link address must have a corresponding target.

The link address in the navigation bar must have the same ID value for the corresponding page element.

Usage 2 --Using JavaScript

Start the scroll listener using JavaScript:

  <script type="text/javascript"> $(function () {  $('.scrollspy-example').scrollspy({ target:'#navbar-example' });  }) </script>

Remove the data-target attribute of the div whose style class is scrollspy-example. In this way, you can switch the scroll wheel.

2. Method

. Scrollspy ('refresh ')

When using the rolling listening plug-in, you need to call this method whenever you add or delete page elements from the DOM in the page, as follows:

Copy codeThe Code is as follows: $ ('[data-spy = "scroll"]'). each (function () {var $ spy = $ (this ). scrollspy ('refresh ')})

3. Options

Options can be passed through the data attribute or JavaScript. For the data attribute, you need to put the option name after data-, for example, data-offset = "".

4. Events

 <script type="text/javascript"> $('#navbar-example').on('activate.bs.scrollspy', function () {   alert(1); }) </script>

Note: you must add a scroll bar to the content of the rolling listener, that is, you must add a style in advance.

  <style type="text/css"> .scrollspy-example   {    height: 200px;    overflow: auto;   position: relative;   border:1px solid red;  } </style>

Give the Div content a certain height.

The above are the learning notes for Bootstrap rolling listening. If you want to continue learning Bootstrap, click here to continue learning. I hope this will help you learn more, we also hope that you will continue to pay attention to more highlights of the customer's home.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.