window.addEventListener('scroll',()=>{
let t = document.documentElement.scrollTop || document.body.scrollTop,
nav = document.getElementsByClassName('nav')[0];
t >= nav.offsetTop ? (nav.classList.add('fixed')) : (nav.classList.remove('fixed'));
})
Hello everyone, the above code cannot remove the class name add normally. . . Even if I reverse the order of add and remove, I still can't execute it;
I have been searching for this problem for a long time but cannot solve it, so I would like to ask for your advice. Thank you thank you.
nav.offsetTop
is0
right.Change
t >= nav.offsetTop
tot > nav.offsetTop
.1. First, take a look at the output of t and nav.offsetTo to make sure these values are the expected values
2. Make sure your scrolling area is on the body and not on a certain p in the body