Skip to content

Commit 0a5c624

Browse files
committed
Fix key press
1 parent 04b094d commit 0a5c624

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

src/components/Header.astro

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import links from "@data/links.json";
1010

1111
<section
1212
id="navbar"
13-
class="fixed top-0 z-50 transition-transform duration-300 transform-gpu w-full"
1413
>
1514
<div
1615
class="container max-w-[1150px] mx-auto px-6 py-2 mt-1 lg:p-2 lg:mt-6 flex items-center justify-between relative z-40 bg-white/80 rounded-full backdrop-blur-md shadow-lg"
@@ -47,6 +46,16 @@ import links from "@data/links.json";
4746
</section>
4847
<Search />
4948

49+
<style>
50+
#navbar {
51+
position: fixed;
52+
top: 0;
53+
width: 100%;
54+
z-index: 1000;
55+
transition: transform 0.3s ease-in-out;
56+
transform: translateY(0);
57+
}
58+
</style>
5059
<script>
5160
document.addEventListener("DOMContentLoaded", function() {
5261
const navbar = document.getElementById("navbar") as HTMLElement;
@@ -64,9 +73,9 @@ document.addEventListener("DOMContentLoaded", function() {
6473
const currentScrollPos = Math.max(0, window.pageYOffset || document.documentElement.scrollTop);
6574
const documentHeight = document.documentElement.scrollHeight;
6675
const windowHeight = window.innerHeight;
67-
68-
// Don't hide navbar when at the very top
69-
if (currentScrollPos <= 10) {
76+
77+
// Don't hide navbar when at the very top - increased threshold for reliability
78+
if (currentScrollPos <= 20) {
7079
if (!isVisible) {
7180
navbar.style.transform = "translateY(0)";
7281
isVisible = true;
@@ -107,16 +116,71 @@ document.addEventListener("DOMContentLoaded", function() {
107116
ticking = false;
108117
}
109118

119+
// Additional polling check when near the top (for edge cases)
120+
let topCheckInterval: any = null;
121+
122+
function startTopCheck() {
123+
if (topCheckInterval) return;
124+
topCheckInterval = setInterval(() => {
125+
const currentScrollPos = Math.max(0, window.pageYOffset || document.documentElement.scrollTop);
126+
if (currentScrollPos <= 20 && !isVisible) {
127+
navbar.style.transform = "translateY(0)";
128+
isVisible = true;
129+
prevScrollPos = currentScrollPos;
130+
}
131+
if (currentScrollPos > 100 && topCheckInterval) {
132+
clearInterval(topCheckInterval);
133+
topCheckInterval = null;
134+
}
135+
}, 50);
136+
}
137+
110138
function requestTick() {
111139
if (!ticking) {
112140
requestAnimationFrame(updateNavbar);
113141
ticking = true;
114142
}
143+
144+
// Start polling when we're in the danger zone
145+
const currentScrollPos = Math.max(0, window.pageYOffset || document.documentElement.scrollTop);
146+
if (currentScrollPos <= 100) {
147+
startTopCheck();
148+
}
115149
}
116150

117151
// Use passive listener for better performance on mobile
118152
window.addEventListener("scroll", requestTick, { passive: true });
119153

154+
// Handle keyboard navigation (arrow keys, page up/down, home/end)
155+
document.addEventListener("keydown", function(e) {
156+
const keysToWatch = ['PageUp', 'PageDown', 'End', 'Home', 'ArrowUp', 'ArrowDown'];
157+
if (keysToWatch.includes(e.key)) {
158+
setTimeout(() => {
159+
const currentScrollPos = Math.max(0, window.pageYOffset || document.documentElement.scrollTop);
160+
if (currentScrollPos <= 10) {
161+
navbar.style.transform = "translateY(0)";
162+
isVisible = true;
163+
prevScrollPos = currentScrollPos;
164+
}
165+
}, 100);
166+
}
167+
});
168+
169+
// Additional check with a longer delay for keyboard scrolling
170+
document.addEventListener("keyup", function(e) {
171+
const keysToWatch = ['PageUp', 'PageDown', 'End', 'Home', 'ArrowUp', 'ArrowDown'];
172+
if (keysToWatch.includes(e.key)) {
173+
setTimeout(() => {
174+
const currentScrollPos = Math.max(0, window.pageYOffset || document.documentElement.scrollTop);
175+
if (currentScrollPos <= 10) {
176+
navbar.style.transform = "translateY(0)";
177+
isVisible = true;
178+
prevScrollPos = currentScrollPos;
179+
}
180+
}, 200);
181+
}
182+
});
183+
120184
// Handle orientation change on mobile devices
121185
window.addEventListener("orientationchange", function() {
122186
setTimeout(() => {

0 commit comments

Comments
 (0)