Computer Science, asked by jahongir0jdfk, 5 hours ago

I don't understand it. Can someone explain it to me. It's not working and I don't know where's error:




window.addEventListener(scroll, function () {
var header = document.querySelector("header");
header.classList.toggle("sticky", window.scrollY > 0);
});

Answers

Answered by tanmayshankarchakrab
0

Answer:

close it and open

Explanation:

Answered by MichMich0945
0

Answer:

The event which you have passed to the addEvenListener function is invalid, the event should be wrapped in ' ( Apostrophe )

That is,

window.addEventListener('scroll', function () {

var header = document.querySelector("header");

header.classList.toggle("sticky", window.scrollY > 0);

});

Also a tip, try using the console to check if there are any errors in your javascript.

I would prefer to initialize the header variable outside the event listener function as the whole function block is executed each time the user scrolls, that also means that your header variable is getting initialized again and again and again. This won't create any performance issue at the moment but if you're developing a huge web page which triggers animations on scroll might make your web page laggy.

Hope this help you!

Similar questions