-
Notifications
You must be signed in to change notification settings - Fork 402
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (28 loc) · 909 Bytes
/
index.js
File metadata and controls
37 lines (28 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const COMPONENT_SELECTOR = '[data-back-scrolling]'
const typewriter = require('analytics')
export default function () {
const components = Array.from(document.querySelectorAll(COMPONENT_SELECTOR))
if (!components.length) return
function handleScroll() {
const scrolled =
document.body.scrollTop > 30 ||
document.documentElement.scrollTop > 30
components.forEach(component => {
const activeClass = component.dataset.activeClass
if (!activeClass) return
component.classList.toggle(activeClass, scrolled)
})
}
// Single optimized scroll listener
window.addEventListener('scroll', handleScroll, { passive: true })
components.forEach(component => {
component.addEventListener('click', e => {
e.preventDefault()
typewriter.scrollToTopClicked()
window.scrollTo({
top: 0,
behavior: 'smooth'
})
})
})
}