From 8d4a13ead2af75ea5319f24e8cb4ef84671211d2 Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Wed, 10 Oct 2018 00:35:17 +0200 Subject: [PATCH] fix: prevent Tabs/Accordion to scroll to others components' anchors Related to https://github.com/zurb/foundation-sites/issues/11527 --- js/foundation.accordion.js | 18 ++++++++++-------- js/foundation.tabs.js | 14 ++++++++------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/js/foundation.accordion.js b/js/foundation.accordion.js index 13ab360d6..a8aa3b43a 100644 --- a/js/foundation.accordion.js +++ b/js/foundation.accordion.js @@ -81,6 +81,8 @@ class Accordion extends Plugin { var $anchor = anchor && $(anchor); var $link = anchor && this.$element.find(`[href$="${anchor}"]`); + // Whether the anchor element that has been found is part of this element + var isOwnAnchor = !!($anchor.length && $link.length); // If there is an anchor for the hash, open it (if not already active) if ($anchor && $link && $link.length) { @@ -93,15 +95,15 @@ class Accordion extends Plugin { this._closeAllTabs(); } - // Roll up a little to show the titles - if (this.options.deepLinkSmudge) { - onLoad($(window), () => { - var offset = this.$element.offset(); - $('html, body').animate({ scrollTop: offset.top }, this.options.deepLinkSmudgeDelay); - }); - } + if (isOwnAnchor) { + // Roll up a little to show the titles + if (this.options.deepLinkSmudge) { + onLoad($(window), () => { + var offset = this.$element.offset(); + $('html, body').animate({ scrollTop: offset.top }, this.options.deepLinkSmudgeDelay); + }); + } - if ($anchor && $link) { /** * Fires when the plugin has deeplinked at pageload * @event Accordion#deeplink diff --git a/js/foundation.tabs.js b/js/foundation.tabs.js index d780646c0..151312461 100644 --- a/js/foundation.tabs.js +++ b/js/foundation.tabs.js @@ -116,6 +116,8 @@ class Tabs extends Plugin { var anchorNoHash = anchor.indexOf('#') >= 0 ? anchor.slice(1) : anchor; var $anchor = anchorNoHash && $(`#${anchorNoHash}`); var $link = anchor && this.$element.find(`[href$="${anchor}"],[data-tabs-target="${anchorNoHash}"]`).first(); + // Whether the anchor element that has been found is part of this element + var isOwnAnchor = !!($anchor.length && $link.length); // If there is an anchor for the hash, select it if ($anchor && $anchor.length && $link && $link.length) { @@ -126,13 +128,13 @@ class Tabs extends Plugin { this._collapse(); } - // Roll up a little to show the titles - if (this.options.deepLinkSmudge) { - var offset = this.$element.offset(); - $('html, body').animate({ scrollTop: offset.top }, this.options.deepLinkSmudgeDelay); - } + if (isOwnAnchor) { + // Roll up a little to show the titles + if (this.options.deepLinkSmudge) { + var offset = this.$element.offset(); + $('html, body').animate({ scrollTop: offset.top }, this.options.deepLinkSmudgeDelay); + } - if ($anchor && $link) { /** * Fires when the plugin has deeplinked at pageload * @event Tabs#deeplink -- 2.47.2