-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from surjithctly/sticky-header
feat: add sticky header utility
- Loading branch information
Showing
4 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
interface Props { | ||
scrollY?: number; | ||
activeClass?: string; | ||
defaultClass?: string; | ||
class?: string; | ||
} | ||
const { | ||
scrollY = 100, | ||
defaultClass = "", | ||
activeClass = "", | ||
class: className = "", | ||
} = Astro.props; | ||
--- | ||
|
||
<header class:list={["astronav-sticky-header", className, defaultClass]}> | ||
<slot /> | ||
</header> | ||
|
||
<script define:vars={{ scrollY, defaultClass, activeClass }}> | ||
let scrollPos = 0; | ||
let ticking = false; | ||
|
||
function OnScroll(scrollPos) { | ||
const headers = document.querySelectorAll(".astronav-sticky-header"); | ||
const classArray = activeClass.split(" "); | ||
const replaceArray = defaultClass.split(" "); | ||
|
||
headers.forEach((header) => { | ||
if (scrollPos > scrollY) { | ||
header.classList.remove(...replaceArray); | ||
header.classList.add("is-active", ...classArray); | ||
header.setAttribute("active", ""); | ||
} | ||
//reduce the scrollY to avoid flickering when scrolling up | ||
if (scrollPos < Math.max(scrollY - 20, 0)) { | ||
header.classList.remove("is-active", ...classArray); | ||
header.classList.add(...replaceArray); | ||
header.removeAttribute("active"); | ||
} | ||
}); | ||
} | ||
|
||
// Scroll event throttling as per MDN | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Document/scroll_event | ||
|
||
document.addEventListener("scroll", (event) => { | ||
scrollPos = window.scrollY; | ||
if (!ticking) { | ||
window.requestAnimationFrame(() => { | ||
OnScroll(scrollPos); | ||
ticking = false; | ||
}); | ||
|
||
ticking = true; | ||
} | ||
}); | ||
</script> |