Created
September 3, 2014 12:40
-
-
Save psymeon/89c25019a64397c51f71 to your computer and use it in GitHub Desktop.
Simple jQuery function that determines whether DOM element is in the browser's viewport.
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
// Custom jQuery function. | |
// Determines if element is withing the viewport. | |
$.fn.inViewport = function() { | |
$el = this; | |
var elTop = $el.offset().top; | |
var elBot = elTop + $el.outerHeight(true); | |
var winScrollTop = $(window).scrollTop(); | |
var winHeight = $(window).height(); | |
var inViewport = winScrollTop <= elBot && (winScrollTop + winHeight) >= elTop; | |
return inViewport; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment