fix: better scroll top appearance for site header and cap negative scroll position

This commit is contained in:
Tim Rijkse
2026-01-15 10:48:31 +01:00
parent 80673fd810
commit d8185591f1

View File

@@ -11,6 +11,7 @@ class SiteHeader extends HTMLElement {
this.prevScrollPos = 0; this.prevScrollPos = 0;
this.headerOffset = 0; this.headerOffset = 0;
this.collapsibleHeight = 0; this.collapsibleHeight = 0;
this.upwardScroll = 0;
this.handleScroll = this.handleScroll.bind(this); this.handleScroll = this.handleScroll.bind(this);
} }
@@ -25,7 +26,7 @@ class SiteHeader extends HTMLElement {
} }
handleScroll() { handleScroll() {
const currentScrollPos = window.scrollY; const currentScrollPos = Math.max(0, window.scrollY);
const isScrollingDown = currentScrollPos > this.prevScrollPos; const isScrollingDown = currentScrollPos > this.prevScrollPos;
const delta = currentScrollPos - this.prevScrollPos; const delta = currentScrollPos - this.prevScrollPos;
@@ -34,15 +35,19 @@ class SiteHeader extends HTMLElement {
} }
if (isScrollingDown) { if (isScrollingDown) {
this.upwardScroll = 0;
this.headerOffset = Math.min( this.headerOffset = Math.min(
this.collapsibleHeight, this.collapsibleHeight,
Math.max(0, this.headerOffset + delta) Math.max(0, this.headerOffset + delta)
); );
this.classList.remove("reveal"); this.classList.remove("reveal");
} else if (delta < 0) { } else if (delta < 0) {
this.upwardScroll += Math.abs(delta);
if (this.upwardScroll >= 20) {
this.headerOffset = 0; this.headerOffset = 0;
this.classList.add("reveal"); this.classList.add("reveal");
} }
}
this.collapsible.style.transform = `translateY(-${this.headerOffset}px)`; this.collapsible.style.transform = `translateY(-${this.headerOffset}px)`;