diff options
author | GeoSot <geo.sotis@gmail.com> | 2022-07-28 11:58:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-28 11:58:28 +0300 |
commit | db86607c088bd307aa21f4b4bd0258262262a4e4 (patch) | |
tree | 7acb80965491822be470802088d0ce85d4087e5c /js/src | |
parent | 90c50ab198a4ecffdda6a5ff10fe58cab2c816b2 (diff) |
ScrollSpy: make the threshold option configurable (#36750)
* feat(ScrollSpy): make the threshold option configurable
Diffstat (limited to 'js/src')
-rw-r--r-- | js/src/scrollspy.js | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index 102a2e1010..f4d6671013 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -40,14 +40,16 @@ const Default = { offset: null, // TODO: v6 @deprecated, keep it for backwards compatibility reasons rootMargin: '0px 0px -25%', smoothScroll: false, - target: null + target: null, + threshold: [0.1, 0.5, 1] } const DefaultType = { offset: '(number|null)', // TODO v6 @deprecated, keep it for backwards compatibility reasons rootMargin: 'string', smoothScroll: 'boolean', - target: 'element' + target: 'element', + threshold: 'array' } /** @@ -110,6 +112,13 @@ class ScrollSpy extends BaseComponent { // TODO: on v6 target should be given explicitly & remove the {target: 'ss-target'} case config.target = getElement(config.target) || document.body + // TODO: v6 Only for backwards compatibility reasons. Use rootMargin only + config.rootMargin = config.offset ? `${config.offset}px 0px -30%` : config.rootMargin + + if (typeof config.threshold === 'string') { + config.threshold = config.threshold.split(',').map(value => Number.parseFloat(value)) + } + return config } @@ -141,8 +150,8 @@ class ScrollSpy extends BaseComponent { _getNewObserver() { const options = { root: this._rootElement, - threshold: [0.1, 0.5, 1], - rootMargin: this._getRootMargin() + threshold: this._config.threshold, + rootMargin: this._config.rootMargin } return new IntersectionObserver(entries => this._observerCallback(entries), options) @@ -187,11 +196,6 @@ class ScrollSpy extends BaseComponent { } } - // TODO: v6 Only for backwards compatibility reasons. Use rootMargin only - _getRootMargin() { - return this._config.offset ? `${this._config.offset}px 0px -30%` : this._config.rootMargin - } - _initializeTargetsAndObservables() { this._targetLinks = new Map() this._observableSections = new Map() |