Last active
January 3, 2016 12:59
-
-
Save AshKyd/8466674 to your computer and use it in GitHub Desktop.
Is a given element on the screen at this time?
This file contains 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
/** | |
* Check whether an element is visible | |
* @param {jQuery} elm jQuery element | |
* @return {Boolean} True if any part of the element is on the screen. | |
*/ | |
function isVisible( ele ) { | |
var viewportTop = $(window).scrollTop(); | |
var viewportBottom = viewportTop + $(window).height(); | |
var eleTop = $(ele).offset().top; | |
var eleBottom = eleTop + $(ele).height(); | |
return eleBottom > viewportTop && eleTop < viewportBottom; | |
} | |
if(window.module && module.exports){ | |
module.exports = isVisible; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment