+ 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?

29th Sep 2022, 12:48 PM
Temmy
Temmy - avatar
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
3rd Oct 2022, 4:45 AM
Niththish
Niththish - avatar