Skip to content

Instantly share code, notes, and snippets.

@artcommacode
Last active August 29, 2015 14:02
Show Gist options
  • Save artcommacode/25bb421af3292b66f17c to your computer and use it in GitHub Desktop.
Save artcommacode/25bb421af3292b66f17c to your computer and use it in GitHub Desktop.
var currentlyIn = false;
// we need somewhere to hold state so that we don't continuously emit events
$(window).bind('scroll', function() {
// bind the scroll event from the window so this function's called when the user scrolls
var inView = ($(this).scrollTop() + $(this).height()) > $('.some-element').offset().top;
// check if the top of the element is less than the bottom of the window
if (inView === currentlyIn) return true;
// if the new state is the same as the old then don't do anything
$('.some-element').trigger(inView ? 'in-view' : 'out-view');
// 'else' trigger an event depending on whether the element has gone in or out of view
currentlyIn = inView;
// update the state
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment