I want to trigger my scroller function only when the window width exceeds 599 pixels and the user is scrolling. The function itself works fine during scrolling, but adding an event listener seems to cause it not to work. Can anyone offer guidance on how to fix this issue?
//only execute scroller if window size is greater than 599
window.addEventListener('resize', function(){
if(window.innerWidth > 599){
window.onscroll = function() {scroller()}
}
};
const scroller = () => {
//PROGRESS SCROLL BAR
var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
var scrolled = (winScroll / height) * 100;
document.getElementById("scrollBar").style.width = scrolled + "%";
//COLLAPSE HEADER WHEN SCROLLING
if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
document.querySelector("header").style.height = "5rem";
document.querySelector("h1").style.display = "none";
document.querySelector("nav ul").style.top = "1rem";
document.getElementById("wrapper").style.marginTop = "10rem";
document.getElementById("scrollContainer").style.top = "5rem";
document.getElementById("scrollBar").style.top = "5rem";
} else {
document.querySelector("header").style.height = "15rem";
document.querySelector("h1").style.display = "inherit";
document.querySelector("nav ul").style.top = "3rem";
document.getElementById("wrapper").style.marginTop = "15rem";
document.getElementById("scrollContainer").style.top = "inherit";
document.getElementById("scrollBar").style.top = "inherit";
}
};