Bootstrap Tutorial JS plugin scrolling monitor learning notes sharing _javascript Tips

Source: Internet
Author: User

This article is mainly to learn about JavaScript plug-ins-scrolling monitoring.

1. Case

The scrolling listener can automatically update the corresponding navigation mark based on the position of the scroll bar. You can try scrolling this page to see the changes to the left navigation.

First the implementation of the code, you can test the code first to see the effect.

<! DOCTYPE html>  

usage 1-- through the Data property

By looking at the page elements that you need to listen to (generally) but adding them to the div above, you can see the code for yourself. Then add attributes to the div data-spy= "Scroll" makes it easy to add scrolling listening to the top navigation bar. The Data-target property is then added to the value of this property to the ID or class of the parent element of any bootstrap. Nav component.

Copy Code code 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 within the navigation bar must have the same ID value for the corresponding page element.

usage 2-- through JavaScript

To start scrolling listening via javascript:

  <script type= "Text/javascript" > $ (function () {
  $ ('. Scrollspy-example '). Scrollspy ({target: ' # Navbar-example '})
  </script>

Remove its data-target attribute by scrollspy-example the style class as a div. This also allows you to switch the mouse wheel.

2. Methods

. Scrollspy (' Refresh ')

When you use a scrolling listener plug-in, you need to call this method whenever you add or remove page elements from the DOM in the page, as follows:

Copy Code code as follows:
$ (' [data-spy= ' scroll] '). each (the function () {var $spy = $ (this). Scrollspy (' Refresh ')})

3. Option

You can pass options through the Data property or JavaScript. For the Data property, the option name needs to be placed after data-, such as data-offset= "".

4. Events

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

Finally note: For scrolling listening content of course to add scrollbars, that is, to add a style

  <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 is bootstrap scrolling listening to the relevant content of learning notes, if you want to continue to learn bootstrap, you can click here, continue to learn, I hope to help you learn, but also hope that you continue to pay attention to the cloud habitat community more wonderful content.

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.