For some reason my ScrollToTop component is not working as expected in my app. I have tried to debug the matter in all ways that i know but i cannot figure out why the component do not log value of scrollY as expected. I tried to move the snippet below in other component of my app but same result. I tried to call the toggleVisibilty function but it only log 0 once then nothing else. Can someone tell me what can cause such error, please? thanks
const toggleVisibility = () => {
console.log(window.scrollY)
// window.pageYOffset >= 300 ?
// setisVisible(true) :
// setisVisible(false)
// console.log(window.pageYOffset)
};
//toggleVisibility()
// const scrollToTop = () => {
// window.scrollTo({
// top:0,
// behavior: "smooth",
// });
// };
useEffect(() => {
window.addEventListener('scroll', toggleVisibility, {passive: true});
return () => window.removeEventListener('scroll', toggleVisibility);
});
I found what was my mistake. my html and body had a height property rather than a min-height in my css file. When changed this it worked fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.