diff --git a/decidim-core/app/packs/src/decidim/sticky_header.js b/decidim-core/app/packs/src/decidim/sticky_header.js index 0b2b238b29c09..b331689cebfa2 100644 --- a/decidim-core/app/packs/src/decidim/sticky_header.js +++ b/decidim-core/app/packs/src/decidim/sticky_header.js @@ -1,17 +1,18 @@ let prevScroll = window.scrollY; const stickyHeader = document.getElementById("sticky-header"); -document.addEventListener("scroll", () => { - // if a subelement is not visible it has no offsetParent - const header = document.getElementById("main-bar").offsetParent; - if (header) { - let currentScroll = window.scrollY; - if (prevScroll > currentScroll || currentScroll < stickyHeader.offsetHeight) { - stickyHeader.style.top = 0; - } else { - stickyHeader.style.top = `-${stickyHeader.offsetHeight}px`; - console.log(header.offsetHeight); +if (stickyHeader) { + document.addEventListener("scroll", () => { + // if a subelement is not visible it has no offsetParent + const header = document.getElementById("main-bar").offsetParent; + if (header) { + let currentScroll = window.scrollY; + if (prevScroll > currentScroll || currentScroll < stickyHeader.offsetHeight) { + stickyHeader.style.top = 0; + } else { + stickyHeader.style.top = `-${stickyHeader.offsetHeight}px`; + } + prevScroll = currentScroll; } - prevScroll = currentScroll; - } -}); + }); +};