Skip to content

Commit

Permalink
add guard clause for sticky header
Browse files Browse the repository at this point in the history
  • Loading branch information
ElviaBth committed Jun 1, 2024
1 parent 13e8094 commit d7c8da7
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions decidim-core/app/packs/src/decidim/sticky_header.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
};

0 comments on commit d7c8da7

Please sign in to comment.