Bootstrap scrolles listening components scrollspy.js how to use

Actually, the case of scroll monitoring is still much, such as navigation is on the right side, when the subject is scrolling a piece, the right navigation corresponds to highlight.

Implementation

1, when the HashKey distance set in the scroll area is associated with the designated item on the navigation

) ] 2, navigation must be .NAV> Li> A structure, and a hREF or DATA-TARGET to bind Hashkey
3, there must be .NAV style
4, scrolling area Data-target Navigate the parent ID (must be a parent) to be consistent.

 One  
One specific content
One specific content
One specific content



ONE specific content
One Specific content
One specific content
One specific content
ONE specific content

Three


ONE specific content
One specific content
One specific content
ONE specific content
One specific content
ONE specific content





Let's take a look at the specific code of the implementation. Principle: When the Hashkey location within the rolling container is the value of only OFFSET settings at the top of the container, It will set the corresponding navigation href highlighted.

ScrollSpy constructor
First, a new constructor as follows:

function ScrollSpy ( .. element, options) {this $ body = $ (document.body) this $ scrollElement = $ (element) .is (document.body) $ (window):? $ (element) this.options = $ .extend ( {}, ScrollSpy.DEFAULTS, options) this.selector = (this.options.target || ”) + ‘.nav li> a’ this.offsets = [] this.targets = [] this.activeTarget = null this .scrollHeight = 0 this. $ scrollElement.on ( ‘scroll.bs.scrollspy’, $ .proxy (this.process, this)) this.refresh () this.process ()}

this constructor is primarily did Han:
1 basic settings, main body or a specific set of a rolling element is an element of this set; followed if the navigation structure .nav. li> a structure, that is, your menu should also have .nav this class.
  2. Listening rolling elements when the process method is executed.  
3. In the meantime, when the initialization is also performedRefresh and Process method.
Let me explain these methods.

acquires the content height of the rolling container (including the hidden portion)

this. $ Scrollelement [0]. ScrollHeight || Math.max (this. $ Body [0] .scrollHeight, Document.documentelement.scrollHeight)

Refresh method

Refresh The value of each hashkey in the rolling container



scrollspy.prototype.refresh = function () {var this = this var offsetMethod = ‘offset’ var OFFSETBASE = 0 this.offsets = [] this.targets = [] this.scrollHeight = this.getscrollHeight () if (! $. Iswindow (this. $ scrollelement [0])) {offsetMethod = ‘position’ offsetbasetBasetBasetBasetBase $ scrollelement.scrolltop ()} this. $ body .find (this.selector) .map (function () {var $ el = $ (this) var href = $ el.data (‘TARGET ‘) || $ el.attr (‘ href ‘) var $ href = / ^ # .. t t r ($ href) Return ($ HREF && $ HREF.LENGTH && $ HREF.IS (“: Visible ‘) &&[[$ HREF [OffsetMethod] (). Top + OffsetBase, HREF]] || null}). Sort (Function (A, B) {RETURN A [0] – B [0]}). Each (function () {That.offsets.push (this [0]) That.targets.push (this [1])})}

It is mainly implemented what?

1. Default with OFFSET to get the positioning value, if the scrolling area is not Window, use position to get
   IF (! $. Iswindow (this. $ SCROLLELEMENT [0])) {offsetMethod = 'position' OffsetBase = this. $ scrollelement.scrolltop () 

2. According to the HashKey on the navigation, the acquisition scroll is traversed. 5Key in the area corresponding to the OFFSET value:

THIS. $ Body .find (this.selector) .map (function () {var $ el = $ (this) Var HRef = $ el.data (‘target’) || $ el.attr (‘href’) var $ href = / ^ # .. t t ($ href) Return ($ href && $ href.Length) ($ HREF && $ HREF.LENGTH && $ HREF.IS (‘: Visible’) &&[[$ HREF [offsetMethod] (). Top + offsetbase, href]]) || null}). Sort (Function (A, B) {Return A [0] – B [0]}). Each (function () {That.offsets.push (this [0]) That.targets.push (this [1])})
   

Rolling bar event trigger function for calculating the navigation menu
   
, is used to calculate the navigation menu


.] ScrollSpy.prototype.process = function () {var scrollTop = this $ scrollElement.scrollTop () + this.options.offset var scrollHeight = this.getScrollHeight () var maxScroll = this.options.offset + scrollHeight – this $. Scrollelement.Height () var offsets = this.offsets var targets = this.targetsvar activeTarget = this.activeTarget var i if (this.scrollHeight = scrollHeight!) {this.refresh ()} if (scrollTop> = maxScroll) {return activeTarget = (i = targets [targets.length – 1])! && this .activate (i)} ac (activetarget && scrolltop
= offsets [i] && (offsets [i + 1] === undefined || scrolltop

Main Role:
  1. Get rolling container has been rolled distance:  < offsets[i + 1])
    && this.activate(targets[i])
  }
 }

 
VAR scrolltop = this. $ Scrollelement.scrolltop () + this.Options.offset

2. The maximum height of the rolling container can scroll:

 VAR MAXSCROLL = this.Options.offset + ScrollHeight -   3. Set the scrolling element logic to add highlights to the current matching element: 

for (i = offsets.Length; I -;) {ActiveTarget! = Targets [i] && scroLLTOP> = Offsets [i] && (Offsets [i + 1] === undefined || scrolltop
 Active method   
Set the specified navigation menu highlight

scrollspy.prototype.activate = function (target) {this.activetarget = target this.clear () var selection = THISSELECTOR + ‘[data-target = “‘ + Target + ‘”],’ + this.selector + ‘[href = “‘ + target + ‘” Rel = “external nofollow” rel = “external nofollow”] “” “EXTERNAL NOFOLLOW” VAR Active = $ (Selector) .parents (‘Li’) .addclass (‘Active’) IF (Active.Parent (‘. Dropdown-Menu’). Length) {Active = Active .closest (‘li.dropdown’) .addclass (‘active’)} Active.trigger (‘Activate.bspy “}
  < offsets[i + 1])
    && this.activate(targets[i])
  }
 
CLEAR method


Clear all highlight menu

scrollspy.prototype.clear= function () {$ (this.selector) .parentsuntil (this.Options.target, ‘.active’) .removeClass (‘active’)}
   
Source


+ Function ($) {‘use strict’; // scrollspy class definition // ========= ===================================================================================================================== • Body “$ (window): $ (element) this.Options = $ .Extend ({}, scrollspy.defaults, options) this.selector = (this.Options.target || ‘) +’ .nav li> A ‘this.offsets = [] this.targets = [] this.activetarget = null this.ScrollHeight = 0 this. $ scrollelement.on (‘ scroll.bs.scrollspy ‘, $ .proxy (this.process, this)) this.refresh () this.process ()} scrollspy.version =’3.3.7’ ScrollSpy.DEFAULTS = {offset: 10} ScrollSpy.prototype.getScrollHeight = function () {return this $ scrollElement [0] .scrollHeight || Math.max (this $ body [0] .scrollHeight,.. document.documentElement.scrollHeight)} ScrollSpy.prototype.refresh = function () {var that = this var offsetMethod = ‘offset’ var offsetBase = 0 this.offsets = [] this.targets = [] this.scrollHeight = this.getScrollHeight () IF (! $ scrollelement [0])) {offsetMethod = ‘position’ OffsetBase = this. $ scrollelement.scrolltop ()} this. $ body .find (this.selector) .map (function () {var $ el = $ (this) var href = $ el.data (‘target’) || $ el.attr (‘href’) var $ href = /^#./.test (Href) && $ (HREF) RETURN ($ HREF && $ HREF.LENGTH && $ HREF.IS (‘: Visible’) &&[[$ HREF [OffsetMethod] (). Top + OffsetBase, HREF]]) || Null}). Sort (Function (A, B) {Return A [0] – B [0]}). Each (function () {That.offsets.push (this [0]) That.targets.push (this [1])})} scrollspy.prototype.process = function () {var . scrollTop = this $ scrollElement.scrollTop () + this.options.offset var scrollHeight = this.getScrollHeight () var maxScroll = this.options.offset + scrollHeight -. this $ scrollElement.height () var offsets = this.offsets var targets = this.targets var activeTarget = this.activeTarget var i if (! this.scrollHeight = scrollHeight) {this.refresh ()} if (scrollTop> = maxScroll) {return activeTarget = (i = targets [targets.length! – 1] && this.activate (i)}}} (ActiveTarget && scrolltop

= offsets [i] && (offsets [i + 1] === undefined || scrolltop

  The above is all the content of this article, I hope to everyone Learning is helpful, I also hope that everyone will support Tumi Cloud.  
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment