Top
Home


Test


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>

<style>
.container{
width: 1px;
height: 2000px;
}
.output{
position: fixed;
top: 40px;
left: 25%;
width: 50%;
padding: 50px;
background-color: tomato;
color: white;
font-weight: bold;
font-size: 80px;
text-align: center;
}

.output.one{
background-color: blue;
}
.output.two{
background-color: green;
}
.output.three{
background-color: orange;
}
.output.four{
background-color: red;
}
</style>
</head>
<body>

<div class="container">
<div class="output"></div>
</div>

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
var $output = $('.output');
var newColorClass;
var currentColorClass;

function changeClass(c){
$output.removeClass('one two three four').addClass(c);
currentColorClass = c;
}

$(window).scroll(function(e) {
var scroll = $(this).scrollTop();

if(scroll > 1000){
newColorClass = 'four';
}else if(scroll > 900){
newColorClass = 'three';
}else if(scroll > 600){
newColorClass = 'two';
}else if(scroll > 300){
newColorClass = 'one';
}else{
newColorClass = '';
}
if(currentColorClass !== newColorClass){
changeClass(newColorClass);
}
});
</script>
</body>
</html>