document.addEventListener("DOMContentLoaded", () => {
// Sentinel element at top
const sentinel = document.createElement("div");
sentinel.style.cssText = "position:absolute;top:0;left:0;height:1px;width:1px;pointer-events:none;";
document.body.prepend(sentinel);
// Toggle scrolled class on body
const observer = new IntersectionObserver(([entry]) => {
const atTop = entry.isIntersecting;
document.body.classList.toggle("header-scrolled", !atTop);
document.body.classList.toggle("header-at-top", atTop);
// Always show header when at top
if (atTop) {
document.body.classList.remove("header-hidden");
document.body.classList.add("header-visible");
}
}, { threshold: 0 });
observer.observe(sentinel);
// =======================
// HIDE / SHOW ON SCROLL LOGIC
// =======================
let lastScrollY = window.scrollY;
let ticking = false;
const SCROLL_THRESHOLD = 10; // Minimum scroll distance to trigger hide/show
function updateHeaderVisibility() {
const currentScrollY = window.scrollY;
const scrollDifference = currentScrollY - lastScrollY;
// If at the very top, always show
if (currentScrollY SCROLL_THRESHOLD) {
if (scrollDifference > 0) {
// Scrolling DOWN - hide header
document.body.classList.add("header-hidden");
document.body.classList.remove("header-visible");
} else {
// Scrolling UP - show header
document.body.classList.remove("header-hidden");
document.body.classList.add("header-visible");
}
lastScrollY = currentScrollY;
}
ticking = false;
}
// Listen to scroll events
window.addEventListener("scroll", () => {
if (!ticking) {
window.requestAnimationFrame(updateHeaderVisibility);
ticking = true;
}
}, { passive: true });
// Initialize as visible
document.body.classList.add("header-visible");
// =======================
// FIX HEADER WRAPPER HEIGHT
// =======================
const headerWrap = document.querySelector(".elementor.elementor-location-header");
const desktopEl = document.querySelector("#desktopHeader");
const mobileEl = document.querySelector("#mobileHeader");
if (!headerWrap) return;
const setWrapHeight = () => {
const isDesktop = window.innerWidth > 1200; // adjust to your Elementor breakpoint
const h = isDesktop ? (desktopEl?.offsetHeight || 0) : (mobileEl?.offsetHeight || 0);
headerWrap.style.height = h + "px";
};
setWrapHeight();
window.addEventListener("resize", setWrapHeight, { passive: true });
// Auto-update if header height changes (menus, fonts, responsive shifts)
if ("ResizeObserver" in window) {
const ro = new ResizeObserver(setWrapHeight);
if (desktopEl) ro.observe(desktopEl);
if (mobileEl) ro.observe(mobileEl);
}
});