I am having issues when it comes to adding a background color when I scroll. Currently my code is working but it is not displaying until I scroll to the end of the div. which is background-overlay.
I would like to activate the class black the moment I scroll over the background-overlay class. Another issue is when I scroll past the div the class is not removing. Is there something I am missing with my below code?
https://jsfiddle.net/e6tfgs0a/2/
Snippet code :
$(function() {
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= $('.background-overlay').offset().top) { // check the offset top
$( ".background-overlay" ).addClass( "black" );
} else if(scroll >= $('.background-overlay').offset().top+$('.background-overlay').height()){ // check the scrollHeight
$( ".background-overlay" ).removeClass( "black" );
}
});
});
.full-height, .page {
height:500px;
}
.black {
background: #000000;
transition: background-color 1s ease;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="page">
</div>
<section class="full-height background-overlay" >
</section>
<div class="page">
</div>