Last active
August 29, 2015 14:02
-
-
Save artcommacode/25bb421af3292b66f17c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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