From f8af5bc3267a1d844cac3e39933c97351b672d0f Mon Sep 17 00:00:00 2001 From: Yunus Emre Alpu Date: Sun, 15 Jan 2023 14:30:11 +0300 Subject: [PATCH] Dynamic ToC added. --- client/themes/default/components/page.vue | 33 ++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/client/themes/default/components/page.vue b/client/themes/default/components/page.vue index 0d1f6473..9a80d916 100644 --- a/client/themes/default/components/page.vue +++ b/client/themes/default/components/page.vue @@ -600,6 +600,7 @@ export default { } this.$store.set('page/mode', 'view') + window.addEventListener('scroll', this.handleScroll) }, mounted () { if (this.$vuetify.theme.dark) { @@ -647,6 +648,9 @@ export default { window.boot.notify('page-ready') }) }, + destroyed () { + window.removeEventListener('scroll', this.handleScroll) + }, methods: { goHome () { window.location.assign('/') @@ -668,6 +672,26 @@ export default { }) } }, + // Highlight the current section items in a sticky table of contents as you scroll down the page. + handleScroll () { + const scrollPosition = window.scrollY + const sections = document.querySelectorAll('h1, h2') + const links = document.querySelectorAll('.v-list-item--link') // .v-list-item--link .v-list-item__title + const current = [] + sections.forEach((el) => { + if (el.offsetTop <= scrollPosition + 5) { + current.push(el) + } + }) + const currentSection = current[current.length - 1] + const id = currentSection && currentSection.id + links.forEach((el) => { + el.classList.remove('titleactive') + if (el.getAttribute('id') === `#${id}`) { + el.classList.add('titleactive') + } + }) + }, pageEdit () { this.$root.$emit('pageEdit') }, @@ -788,5 +812,12 @@ export default { } } } - +.titleactive { + background-color:#E1F5FE !important; + // add animation + transition: background-color 0.5s ease; + .v-list-item__title { + color: #01579B !important; + } +}