From b9fde594eb3773b070483244d58ea5a6c3197633 Mon Sep 17 00:00:00 2001 From: Sandro Ducceschi Date: Tue, 31 Oct 2023 15:56:59 +0100 Subject: [PATCH] MODIFIED: make ancho rjump repeatable --- src/App.vue | 37 +++++++++++++++++---- src/components/blocks/ResponsiveGallery.vue | 5 +-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/App.vue b/src/App.vue index 1e0e8c2..b92a14e 100644 --- a/src/App.vue +++ b/src/App.vue @@ -83,22 +83,22 @@ export default { // created() {}, // beforeMount() {}, // render(h) { return h(); }, - // mounted() {}, + mounted() { + this.checkForAnchorLinks(); + }, // beforeUpdate() {}, updated() { if (this.hash) { this.$nextTick(() => { const { samePage } = this; let { hash } = this; - hash = hash.substr(1); - const element = document.querySelector(`[data-slug='${hash}']`); - if (element) { - const options = { behavior: 'smooth', block: 'center', inline: 'center' }; - options.block = samePage ? 'start' : 'center'; - element.scrollIntoView(options); + hash = hash.substr(1) || ''; + if (hash.length > 0) { + this.scrollToAnchor(hash, samePage); } }); } + this.checkForAnchorLinks(); }, // beforeDestroy() {}, // destroyed() {}, @@ -108,6 +108,29 @@ export default { // //--------------------------------------------------- methods: { + checkForAnchorLinks() { + this.$nextTick(() => { + const a = document.querySelectorAll('a[href*="#"]'); + a.forEach((o) => { + o.addEventListener('click', this.handleAnchorLink); + }); + }); + }, + handleAnchorLink(evt) { + const href = evt.target?.href.split('#')[1] || ''; + const hash = window.location.hash.substr(1) || ''; + if (href !== '' && hash !== '' && href === hash) { + this.scrollToAnchor(hash); + } + }, + scrollToAnchor(anchor, samePage = true) { + const element = document.querySelector(`[data-slug='${anchor}'], #${anchor}`); + if (element) { + const options = { behavior: 'smooth', block: 'center', inline: 'center' }; + options.block = samePage ? 'start' : 'center'; + element.scrollIntoView(options); + } + }, handleManageCookies() { this.$refs.cookieNotice.show(); }, diff --git a/src/components/blocks/ResponsiveGallery.vue b/src/components/blocks/ResponsiveGallery.vue index e93bec2..40819de 100644 --- a/src/components/blocks/ResponsiveGallery.vue +++ b/src/components/blocks/ResponsiveGallery.vue @@ -1,5 +1,5 @@