+ 1
javascript
window.addEventListener("scroll", function(){ const header = document.querySelector("header"); header.classList.toggle("sticky", window.scrollY > 0); }); i have been trying this particular code but the "sticky" class was not added upon inspecting, pls where is the mistake?
1 Answer
0
The toggle method takes only one argument but you have provided two.
window.addEventListener("scroll", () => {
const header = document.querySelector("header");
header.classList.toggle("sticky");
});
Also make sure you have
.sticky{
position : sticky;
}
in your css file