I am working on a wordpress website on localhost and using plugin script and style for custom javascript. I want to change the background color when scroll value changes e.g.
scroll value 0-100 then color red
scroll value 100-200 then color blue
scroll value 200-300 then color pink
scroll value 300-400 then color black
scroll value 400-500 then color orange
by this code i am able to add only one class in first condition of javascript.
.site-main{
background-color:green !important;
}
.scrolled1{
background-color:yellow !important;
}
.scrolled2{
background-color:pink !important;
}
.scrolled3{
background-color:black !important;
}
.scrolled4{
background-color:orange !important;
}
.scrolled5{
background-color:red !important;
}
<script>
alert("success");
window.onscroll = function() {myFunction()};
function myFunction() {
if((document.body.scrollTop > 0 || document.documentElement.scrollTop > 0) && (document.body.scrollTop < 100 || document.documentElement.scrollTop < 100)){
document.getElementById("main").className = "scrolled2";
}
else if((document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) && (document.body.scrollTop < 200 || document.documentElement.scrollTop < 200)){
document.getElementById("main").className = "scrolled3";
}
else {
document.getElementById("main").className = "site-main";
}
}
</script>