I'd like to stack elements and when they come into view, it adds the class .active. I don't want the class to be removed, so once it's added it stays there.
General idea:
If .default is in view on scroll add the class .active
So as you scroll down it adds the class when it comes into view.
After looking at similar questions I've come up with this: http://jsfiddle.net/x05vwb28/
$(window).scroll(function() {
if (checkVisible($('.default'))) {
$('.default').addClass('active');
}
});
function checkVisible( elm, eval ) {
eval = eval || "visible";
var vpH = $(window).height(), // Viewport Height
st = $(window).scrollTop(), // Scroll Top
y = $(elm).offset().top,
elementHeight = $(elm).height();
if (eval == "visible") return ((y < (vpH + st)) && (y > (st - elementHeight)));
if (eval == "above") return ((y < (vpH + st)));
}
It half works... When scrolled it adds the active class to all elements instead of only to the one in view.
I'd like the first div to automatically add the class of active (because it's already in view)
And if I'm honest, as much as it kind of works... I don't understand the function.
Is there an easier way to do this?