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.