Skip to content

Commit 2d51195

Browse files
committed
wrap around requestAnimationFrame
1 parent 4b00ad1 commit 2d51195

File tree

1 file changed

+43
-42
lines changed
  • packages/frontendmu-nuxt/components/site

1 file changed

+43
-42
lines changed

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

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -108,52 +108,53 @@ function toggleHeader() {
108108
109109
if (headerElement) {
110110
const handleScroll = () => {
111-
const headerHeight = headerElement.clientHeight;
112-
113-
// the current vertical scroll position of the page
114-
const currentScrollPosition =
115-
window.scrollY || document.documentElement.scrollTop;
116-
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),
123-
headerHeight
124-
);
125-
126-
// checks if the user has scrolled past the header and nextHeaderOffset differs from the current position
127-
if (
128-
currentScrollPosition >= headerHeight &&
129-
nextHeaderOffset !== headerOffset
130-
) {
131-
headerOffset = nextHeaderOffset;
132-
headerElement.style.transform = `translateY(-${headerOffset}px)`;
133-
}
134-
135-
// if the user has scrolled past the header, we add these classes
136-
if (currentScrollPosition > headerHeight) {
137-
headerElement.classList.add(
138-
"intersect",
139-
"shadow-sm",
140-
"dark:bg-verse-900/50",
141-
"bg-verse-50/50"
142-
);
143-
} else {
144-
headerElement.classList.remove(
145-
"intersect",
146-
"shadow-sm",
147-
"dark:bg-verse-900/50",
148-
"bg-verse-50/50"
111+
window.requestAnimationFrame(() => {
112+
const headerHeight = headerElement.clientHeight;
113+
114+
// the current vertical scroll position of the page
115+
const currentScrollPosition =
116+
window.scrollY || document.documentElement.scrollTop;
117+
118+
// the distance that the user has scrolled since the last scroll event
119+
const distance = currentScrollPosition - previousScrollPosition;
120+
121+
// New vertical position of the header
122+
const nextHeaderOffset = Math.min(
123+
Math.max(headerOffset + distance, 0),
124+
headerHeight
149125
);
150-
}
151126
152-
previousScrollPosition = currentScrollPosition;
127+
// checks if the user has scrolled past the header and nextHeaderOffset differs from the current position
128+
if (
129+
currentScrollPosition >= headerHeight &&
130+
nextHeaderOffset !== headerOffset
131+
) {
132+
headerOffset = nextHeaderOffset;
133+
headerElement.style.transform = `translateY(-${headerOffset}px)`;
134+
}
135+
136+
// if the user has scrolled past the header, we add these classes
137+
if (currentScrollPosition > headerHeight) {
138+
headerElement.classList.add(
139+
"intersect",
140+
"shadow-sm",
141+
"dark:bg-verse-900/50",
142+
"bg-verse-50/50"
143+
);
144+
} else {
145+
headerElement.classList.remove(
146+
"intersect",
147+
"shadow-sm",
148+
"dark:bg-verse-900/50",
149+
"bg-verse-50/50"
150+
);
151+
}
152+
153+
previousScrollPosition = currentScrollPosition;
154+
});
153155
};
154156
155-
console.log("test");
156-
window.addEventListener("scroll", handleScroll);
157+
window.addEventListener("scroll", handleScroll, { passive: true });
157158
}
158159
}
159160

0 commit comments

Comments
 (0)