diff options
| author | Mark Otto <[email protected]> | 2017-05-26 22:28:09 -0700 |
|---|---|---|
| committer | Mark Otto <[email protected]> | 2017-05-26 22:28:09 -0700 |
| commit | 6c3f833076a9fa68601741e3e21bd07ad79b7d8a (patch) | |
| tree | fe016946d77f9ffff15bbe9cdc593fd098b5bcc7 /js/src/scrollspy.js | |
| parent | c581564a780974c6430ac5897740006f623f2277 (diff) | |
| parent | 5d7db507396275fcda96935aff47b09e1d79ddc1 (diff) | |
| download | bootstrap-6c3f833076a9fa68601741e3e21bd07ad79b7d8a.tar.xz bootstrap-6c3f833076a9fa68601741e3e21bd07ad79b7d8a.zip | |
Merge branch 'v4-docs-streamlined' of https://github.com/twbs/bootstrap into v4-docs-streamlined
Diffstat (limited to 'js/src/scrollspy.js')
| -rw-r--r-- | js/src/scrollspy.js | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index a664090fb..0a4708bf9 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -3,7 +3,7 @@ import Util from './util' /** * -------------------------------------------------------------------------- - * Bootstrap (v4.0.0-alpha.4): scrollspy.js + * Bootstrap (v4.0.0-alpha.5): scrollspy.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * -------------------------------------------------------------------------- */ @@ -18,7 +18,7 @@ const ScrollSpy = (($) => { */ const NAME = 'scrollspy' - const VERSION = '4.0.0-alpha.4' + const VERSION = '4.0.0-alpha.5' const DATA_KEY = 'bs.scrollspy' const EVENT_KEY = `.${DATA_KEY}` const DATA_API_KEY = '.data-api' @@ -87,7 +87,7 @@ const ScrollSpy = (($) => { this._activeTarget = null this._scrollHeight = 0 - $(this._scrollElement).on(Event.SCROLL, $.proxy(this._process, this)) + $(this._scrollElement).on(Event.SCROLL, (event) => this._process(event)) this.refresh() this._process() @@ -108,13 +108,13 @@ const ScrollSpy = (($) => { // public refresh() { - let autoMethod = this._scrollElement !== this._scrollElement.window ? + const autoMethod = this._scrollElement !== this._scrollElement.window ? OffsetMethod.POSITION : OffsetMethod.OFFSET - let offsetMethod = this._config.method === 'auto' ? + const offsetMethod = this._config.method === 'auto' ? autoMethod : this._config.method - let offsetBase = offsetMethod === OffsetMethod.POSITION ? + const offsetBase = offsetMethod === OffsetMethod.POSITION ? this._getScrollTop() : 0 this._offsets = [] @@ -122,12 +122,12 @@ const ScrollSpy = (($) => { this._scrollHeight = this._getScrollHeight() - let targets = $.makeArray($(this._selector)) + const targets = $.makeArray($(this._selector)) targets .map((element) => { let target - let targetSelector = Util.getSelectorFromElement(element) + const targetSelector = Util.getSelectorFromElement(element) if (targetSelector) { target = $(targetSelector)[0] @@ -196,33 +196,39 @@ const ScrollSpy = (($) => { ) } + _getOffsetHeight() { + return this._scrollElement === window ? + window.innerHeight : this._scrollElement.offsetHeight + } + _process() { - let scrollTop = this._getScrollTop() + this._config.offset - let scrollHeight = this._getScrollHeight() - let maxScroll = this._config.offset + const scrollTop = this._getScrollTop() + this._config.offset + const scrollHeight = this._getScrollHeight() + const maxScroll = this._config.offset + scrollHeight - - this._scrollElement.offsetHeight + - this._getOffsetHeight() if (this._scrollHeight !== scrollHeight) { this.refresh() } if (scrollTop >= maxScroll) { - let target = this._targets[this._targets.length - 1] + const target = this._targets[this._targets.length - 1] if (this._activeTarget !== target) { this._activate(target) } + return } - if (this._activeTarget && scrollTop < this._offsets[0]) { + if (this._activeTarget && scrollTop < this._offsets[0] && this._offsets[0] > 0) { this._activeTarget = null this._clear() return } for (let i = this._offsets.length; i--;) { - let isActiveTarget = this._activeTarget !== this._targets[i] + const isActiveTarget = this._activeTarget !== this._targets[i] && scrollTop >= this._offsets[i] && (this._offsets[i + 1] === undefined || scrollTop < this._offsets[i + 1]) @@ -244,7 +250,7 @@ const ScrollSpy = (($) => { `${selector}[href="${target}"]` }) - let $link = $(queries.join(',')) + const $link = $(queries.join(',')) if ($link.hasClass(ClassName.DROPDOWN_ITEM)) { $link.closest(Selector.DROPDOWN).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE) @@ -252,7 +258,7 @@ const ScrollSpy = (($) => { } else { // todo (fat) this is kinda sus... // recursively add actives to tested nav-links - $link.parents(Selector.LI).find(Selector.NAV_LINKS).addClass(ClassName.ACTIVE) + $link.parents(Selector.LI).find(`> ${Selector.NAV_LINKS}`).addClass(ClassName.ACTIVE) } $(this._scrollElement).trigger(Event.ACTIVATE, { @@ -269,8 +275,8 @@ const ScrollSpy = (($) => { static _jQueryInterface(config) { return this.each(function () { - let data = $(this).data(DATA_KEY) - let _config = typeof config === 'object' && config || null + let data = $(this).data(DATA_KEY) + const _config = typeof config === 'object' && config if (!data) { data = new ScrollSpy(this, _config) @@ -297,10 +303,10 @@ const ScrollSpy = (($) => { */ $(window).on(Event.LOAD_DATA_API, () => { - let scrollSpys = $.makeArray($(Selector.DATA_SPY)) + const scrollSpys = $.makeArray($(Selector.DATA_SPY)) for (let i = scrollSpys.length; i--;) { - let $spy = $(scrollSpys[i]) + const $spy = $(scrollSpys[i]) ScrollSpy._jQueryInterface.call($spy, $spy.data()) } }) |
