Skip to content

Commit e616ea1

Browse files
committed
Update navbar
1 parent e7eceac commit e616ea1

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

src/components/Header.astro

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,40 @@ import links from "@data/links.json";
4848
<Search />
4949

5050
<script>
51-
document.addEventListener("DOMContentLoaded", () => {
52-
let prevScrollPos = window.pageYOffset;
53-
const navbar = document.getElementById("navbar") as HTMLElement;
54-
55-
window.addEventListener("scroll", () => {
56-
let currentScrollPos = window.pageYOffset;
57-
if (prevScrollPos > currentScrollPos) {
58-
navbar.style.transform = "translateY(0)";
59-
} else {
60-
navbar.style.transform = "translateY(-100%)";
51+
document.addEventListener("DOMContentLoaded", function() {
52+
var navbar = document.getElementById("navbar") as HTMLElement;
53+
if (!navbar) return;
54+
55+
var prevScrollPos = window.pageYOffset || document.documentElement.scrollTop;
56+
var isFocused = false;
57+
58+
window.addEventListener("scroll", function() {
59+
var currentScrollPos = window.pageYOffset || document.documentElement.scrollTop;
60+
61+
if (!isFocused) {
62+
if (prevScrollPos > currentScrollPos) {
63+
navbar.style.transform = "translateY(0)";
64+
} else {
65+
navbar.style.transform = "translateY(-100%)";
66+
}
6167
}
68+
6269
prevScrollPos = currentScrollPos;
6370
});
64-
65-
navbar.addEventListener("focusin", () => {
66-
navbar.style.transform = "translateY(0)";
67-
});
68-
69-
navbar.addEventListener("focusout", () => {
70-
if (window.pageYOffset > 100) {
71-
navbar.style.transform = "translateY(-100%)";
72-
}
73-
});
71+
72+
if ('onfocusin' in navbar) {
73+
navbar.addEventListener("focusin", function() {
74+
isFocused = true;
75+
navbar.style.transform = "translateY(0)";
76+
});
77+
78+
navbar.addEventListener("focusout", function() {
79+
isFocused = false;
80+
var currentScrollPos = window.pageYOffset || document.documentElement.scrollTop;
81+
if (currentScrollPos > prevScrollPos) {
82+
navbar.style.transform = "translateY(-100%)";
83+
}
84+
});
85+
}
7486
});
7587
</script>

0 commit comments

Comments
 (0)