I have minimal to no experience with HTML/CSS/JS so most of this is copy and pasted. I am using a website building service called cargo. I am trying to change the background color based on the scroll position of the user. Everything works perfectly when starting at the top and scrolling down. But scrolling up does not allow the colors to change at all (except for the "body" color once you reach the top).
HTML/JS:
<script>
function changeBackground(){
if(scrollY >= 300 && scrollY < 900) {
document.body.classList.add('bg-color');
}
else if(scrollY >= 900 && scrollY < 1900){
document.body.classList.add('bg-color-orangeyellow');
}
else if(scrollY >=1900 && scrollY < 2700){
document.body.classList.add('bg-color-white');
}
else{
document.body.classList.remove('bg-color');
document.body.classList.remove('bg-color-orangeyellow');
document.body.classList.remove('bg-color-white');
}
}
addEventListener('scroll', changeBackground);
</script>
CSS:
body {
background-color:rgb(166, 199, 255);
transition: all 0.5s ease;
}
.bg-color{
background-color: #11923d;
}
.bg-color-orangeyellow{
background-color: #ffb217;
}
.bg-color-white{
background-color: white;