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.
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.