Skip to content

Commit 4b00ad1

Browse files
committed
fix naming and add some helpful comments
1 parent 8b34db8 commit 4b00ad1

File tree

1 file changed

+28
-24
lines changed
  • packages/frontendmu-nuxt/components/site

1 file changed

+28
-24
lines changed

packages/frontendmu-nuxt/components/site/Menu.vue

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,58 +98,62 @@ const links: TMenu = {
9898
};
9999
100100
function toggleHeader() {
101-
const $header = document.querySelector(".menu-wrapper");
102-
let currentTransition = 0;
103-
let lastScrollPosition = 0;
104-
if ($header) {
101+
const headerElement = document.querySelector(".menu-wrapper") as HTMLElement;
102+
103+
// headerOffset tracks how much the header has been moved vertically
104+
let headerOffset = 0;
105+
106+
// This keeps track of the previous scroll position to calculate whether the user is scrolling up or down.
107+
let previousScrollPosition = 0;
108+
109+
if (headerElement) {
105110
const handleScroll = () => {
106-
const headerHeight = $header.clientHeight;
111+
const headerHeight = headerElement.clientHeight;
112+
113+
// the current vertical scroll position of the page
107114
const currentScrollPosition =
108115
window.scrollY || document.documentElement.scrollTop;
109-
const distance = currentScrollPosition - lastScrollPosition;
110116
111-
const nextTransition = Math.min(
112-
Math.max(currentTransition + distance, 0),
117+
// the distance that the user has scrolled since the last scroll event
118+
const distance = currentScrollPosition - previousScrollPosition;
119+
120+
// New vertical position of the header
121+
const nextHeaderOffset = Math.min(
122+
Math.max(headerOffset + distance, 0),
113123
headerHeight
114124
);
115125
126+
// checks if the user has scrolled past the header and nextHeaderOffset differs from the current position
116127
if (
117128
currentScrollPosition >= headerHeight &&
118-
nextTransition !== currentTransition
129+
nextHeaderOffset !== headerOffset
119130
) {
120-
currentTransition = nextTransition;
121-
$header.style.transform = `translateY(-${currentTransition}px)`;
131+
headerOffset = nextHeaderOffset;
132+
headerElement.style.transform = `translateY(-${headerOffset}px)`;
122133
}
123134
135+
// if the user has scrolled past the header, we add these classes
124136
if (currentScrollPosition > headerHeight) {
125-
$header.classList.add(
137+
headerElement.classList.add(
126138
"intersect",
127139
"shadow-sm",
128140
"dark:bg-verse-900/50",
129141
"bg-verse-50/50"
130142
);
131143
} else {
132-
$header.classList.remove(
144+
headerElement.classList.remove(
133145
"intersect",
134146
"shadow-sm",
135147
"dark:bg-verse-900/50",
136148
"bg-verse-50/50"
137149
);
138150
}
139151
140-
lastScrollPosition = currentScrollPosition;
152+
previousScrollPosition = currentScrollPosition;
141153
};
142154
143-
const observer = new IntersectionObserver(
144-
([entry]) => {
145-
if (entry.isIntersecting) {
146-
window.addEventListener("scroll", handleScroll, { passive: true });
147-
}
148-
},
149-
{ threshold: 0 }
150-
);
151-
152-
observer.observe($header);
155+
console.log("test");
156+
window.addEventListener("scroll", handleScroll);
153157
}
154158
}
155159

0 commit comments

Comments
 (0)