I'm missing something with this code block, but I do not know what. I want to detect when an element is within a viewport and add a class to that element. My math being: taking the position of the element on the page, minus the window height, which (if down the page) results in the distance between the page fold and the element itself. If that distance is greater than or equal to the amount scrolled, then add class.
var windowHeight = $(window).height();
var yPos = $(window).scrollTop();
var element = $('#element').offset().top;
if(element - windowHeight >= yPos) {
$('#element').addClass('scrolled');
} else {
$('#element').removeClass('scrolled');
}