diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index 0995eac5d9a0..bd9dd5c14bbb 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -168,14 +168,19 @@ class ScrollSpy extends BaseComponent { } let maxIntersectionRatio = 0 + let element = null for (const [key, val] of this._intersectionRatio.entries()) { if (val > maxIntersectionRatio) { - const element = targetElement(key) - this._activeTarget = element + element = targetElement(key) maxIntersectionRatio = val } } + if (element !== null) { + this._process(element) + return + } + if (this._activeTarget !== null) { this._process(this._activeTarget) } @@ -208,6 +213,11 @@ class ScrollSpy extends BaseComponent { this._activateParents(target) target.classList.add(CLASS_NAME_ACTIVE) + if (target === this._activeTarget) { + return + } + + this._activeTarget = target EventHandler.trigger(this._element, EVENT_ACTIVATE, { relatedTarget: target }) }