JS CODE:
window.onscroll = function() {myFunction();};
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky");
} else {
navbar.classList.remove("sticky");
}
}
What's wrong with this? I found a w3school example of sticky navigation. In the console it displays:
Uncaught TypeError: Cannot read property 'offsetTop' of null