I try to simplify my code with a const but it isn't working
// my const 'myscroll' is not working ?? Why ??
const myscroll = document.querySelector('.scrollup');
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function () {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) {
myscroll.style.display = "block";
} else {
myscroll.style.display = "none";
}
};
I have to do it that way
window.onscroll = function () {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) {
document.querySelector('.scrollup').style.display = "block";
} else {
document.querySelector('.scrollup').style.display = "none";
}
};
It must be so simple but I'm blind ! Thanks for your helps