So adding and removing the "sticky"-class to the navbar works perfectly fine. Addding and removing the "i" class doesn't, though. It either remains the whole time or is "removed" the whole time, depending on the navbar.offsetTop when the page is loaded.
I don't get why (and how to make it work correctly). Can you please help?
<div id="navbar-brand">
<a class="navbar-brand" href="#">Innere Kinder</a>
</div>
.i {
display: none;
}
var navbar = document.getElementById("sticky_navbar");
var brand = document.getElementById("navbar-brand");
var sticky = navbar.offsetTop;
function stickyNavbar() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky");
brand.classList.remove("i");
} else {
navbar.classList.remove("sticky");
brand.classList.add("i");
}
}
function init() {
window.addEventListener('scroll', stickyNavbar, false);
}
document.addEventListener('load', init, false);