Skip to content

Commit 20f20b5

Browse files
committed
Enhance sticky navigation behavior and smooth scrolling adjustments
1 parent fecfaf0 commit 20f20b5

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

js/main.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ document.addEventListener("DOMContentLoaded", function () {
55

66
// Basic sticky navigation
77
const handleScroll = () => {
8-
if (window.scrollY > heroSection.offsetHeight) {
8+
const scrollPosition = window.scrollY;
9+
if (scrollPosition > heroSection.offsetHeight) {
910
nav.classList.add("sticky");
1011
} else {
1112
nav.classList.remove("sticky");
@@ -25,19 +26,18 @@ document.addEventListener("DOMContentLoaded", function () {
2526
// Close menu
2627
navCheckbox.checked = false;
2728

28-
// Calculate offset based on section
29-
const offset = targetId === "#contact" ? 0 : 80;
29+
// Calculate proper offset including nav height
30+
const navHeight = nav.offsetHeight;
31+
const offset = targetId === "#contact" ? navHeight : navHeight + 32;
3032

31-
// Smooth scroll
33+
// Smooth scroll with adjusted position
3234
window.scrollTo({
3335
top: targetSection.offsetTop - offset,
3436
behavior: "smooth",
3537
});
3638

37-
// Update URL after scroll completes
38-
setTimeout(() => {
39-
window.location.hash = targetId;
40-
}, 800);
39+
// Update URL without jumping
40+
window.history.pushState(null, "", targetId);
4141
};
4242

4343
// Event listeners

styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
transition: all 0.3s ease;
4545
}
4646

47+
html {
48+
scroll-padding-top: 100px; /* Prevents content from going under sticky nav */
49+
}
50+
4751
.nav-container.sticky {
4852
position: fixed;
4953
top: 16px;

0 commit comments

Comments
 (0)