Skip to content

Commit 6af5467

Browse files
committed
main
1 parent bd1d135 commit 6af5467

1 file changed

Lines changed: 47 additions & 16 deletions

File tree

src/views/home.vue

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -402,33 +402,64 @@ const registerReveal = (el) => {
402402
}
403403
404404
let sectionObserver
405+
let anchorLinkHandlers = []
405406
onMounted(() => {
406-
if (typeof window === 'undefined' || !('IntersectionObserver' in window)) {
407-
return
407+
if (typeof window !== 'undefined' && 'IntersectionObserver' in window) {
408+
hasRevealObserver.value = true
409+
sectionObserver = new IntersectionObserver(
410+
(entries) => {
411+
for (const entry of entries) {
412+
if (entry.isIntersecting) {
413+
entry.target.classList.add('is-visible')
414+
sectionObserver.unobserve(entry.target)
415+
}
416+
}
417+
},
418+
{ threshold: 0.16, rootMargin: '0px 0px -8% 0px' },
419+
)
420+
421+
for (const node of revealNodes.value) {
422+
sectionObserver.observe(node)
423+
}
408424
}
409425
410-
hasRevealObserver.value = true
411-
sectionObserver = new IntersectionObserver(
412-
(entries) => {
413-
for (const entry of entries) {
414-
if (entry.isIntersecting) {
415-
entry.target.classList.add('is-visible')
416-
sectionObserver.unobserve(entry.target)
417-
}
426+
const anchorLinks = Array.from(document.querySelectorAll('a[href^="#"]'))
427+
anchorLinkHandlers = anchorLinks.map((link) => {
428+
const handler = (event) => {
429+
const href = link.getAttribute('href')
430+
if (!href || href === '#') {
431+
return
418432
}
419-
},
420-
{ threshold: 0.16, rootMargin: '0px 0px -8% 0px' },
421-
)
422433
423-
for (const node of revealNodes.value) {
424-
sectionObserver.observe(node)
425-
}
434+
const target = document.querySelector(href)
435+
if (!target) {
436+
return
437+
}
438+
439+
event.preventDefault()
440+
const targetTop = target.getBoundingClientRect().top + window.scrollY
441+
window.scrollTo({
442+
top: Math.max(0, targetTop - 88),
443+
behavior: 'smooth',
444+
})
445+
target.classList.add('is-visible')
446+
history.replaceState(null, '', href)
447+
}
448+
449+
link.addEventListener('click', handler)
450+
return { link, handler }
451+
})
426452
})
427453
428454
onBeforeUnmount(() => {
429455
if (sectionObserver) {
430456
sectionObserver.disconnect()
431457
}
458+
459+
for (const item of anchorLinkHandlers) {
460+
item.link.removeEventListener('click', item.handler)
461+
}
462+
anchorLinkHandlers = []
432463
})
433464
</script>
434465

0 commit comments

Comments
 (0)