diff options
author | GeoSot <geo.sotis@gmail.com> | 2022-07-16 14:52:12 +0300 |
---|---|---|
committer | GeoSot <geo.sotis@gmail.com> | 2022-07-27 17:40:21 +0300 |
commit | 16723b52330370e97d48aa6470aed6bb02329e9a (patch) | |
tree | 704a275ac523260e026ed33273e6531814d611c8 | |
parent | dfae892801ffc194de6aec34d543d908db3dd8e1 (diff) |
feat(ScrollSpy): make the threshold option configurable
-rw-r--r-- | js/src/scrollspy.js | 8 | ||||
-rw-r--r-- | js/tests/unit/scrollspy.spec.js | 18 |
2 files changed, 23 insertions, 3 deletions
diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index 102a2e1010..83080e800f 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' } /** @@ -141,7 +143,7 @@ class ScrollSpy extends BaseComponent { _getNewObserver() { const options = { root: this._rootElement, - threshold: [0.1, 0.5, 1], + threshold: this._config.threshold, rootMargin: this._getRootMargin() } diff --git a/js/tests/unit/scrollspy.spec.js b/js/tests/unit/scrollspy.spec.js index 2bdeb5830c..61edeb921a 100644 --- a/js/tests/unit/scrollspy.spec.js +++ b/js/tests/unit/scrollspy.spec.js @@ -126,6 +126,24 @@ describe('ScrollSpy', () => { expect(scrollSpy._rootElement).toBeNull() }) + it('should respect threshold option', () => { + fixtureEl.innerHTML = [ + '<ul id="navigation" class="navbar">', + ' <a class="nav-link active" id="one-link" href="#">One</a>' + + '</ul>', + '<div id="content">', + ' <div id="one-link" style="height: 300px;">test</div>', + '</div>' + ].join('') + + const scrollSpy = new ScrollSpy('#content', { + target: '#navigation', + threshold: [1] + }) + + expect(scrollSpy._observer.thresholds).toEqual([1]) + }) + it('should not take count to not visible sections', () => { fixtureEl.innerHTML = [ '<nav id="navigation" class="navbar">', |