I have added a div that has a fixed class added to it, and the class is removed when the scroll position reaches 0, but the content under the fixed div jumps and I'm not sure how to fix that.
Here is my HTML structure:
<div class="announcement-wrapper">
<div class="announcement header-announcement">
{{ settings.announcement_content_seksy }}
</div>
</div>
Here is my Vanilla JS:
let scrollpos = window.scrollY
const header = document.querySelector(".announcement")
const header_height = header.offsetHeight
const add_class_on_scroll = () => header.classList.add("fixed")
const remove_class_on_scroll = () => header.classList.remove("fixed")
window.addEventListener('scroll', function() {
scrollpos = window.scrollY
if (scrollpos >= header_height) { add_class_on_scroll() }
else { remove_class_on_scroll() }
console.log(scrollpos)
})
CSS for the fixed div:
.fixed {
position: fixed;
top:0;
width: 100%;
z-index: 9999;
}
I have seen similar questions asked but I haven't been able to add anything to fix the content that jumps underneath.