I'm trying to detect if a class is currently visible in the viewport. I'm using this answer (can't get a reply to my question). The original post used ID's instead of classes, I've changed it to detect a class called .white-section
which is repeated down the page.
At the moment it only detects the first .white-section
, how can I get it to detect them all as they come and go into the viewport?
$('#content').scroll(function() {
var top_of_element = $(".white-section").offset().top;
var bottom_of_element = $(".white-section").offset().top + $(".white-section").outerHeight();
var bottom_of_screen = $(window).scrollTop() + $(window).height();
var top_of_screen = $(window).scrollTop();
if((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){
$('.header-container').addClass('.alt-menu');
}
else {
$('.header-container').removeClass('.alt-menu');
}
});
This is how the html is setup:
<div class="header-container"></div>
<div class="white-section"></div>
<div class="white-section"></div>
<div class="white-section"></div>
<div class="white-section"></div>
<div class="white-section"></div>