Skip to content

Instantly share code, notes, and snippets.

@AshKyd
Last active January 3, 2016 12:59
Show Gist options
  • Save AshKyd/8466674 to your computer and use it in GitHub Desktop.
Save AshKyd/8466674 to your computer and use it in GitHub Desktop.
Is a given element on the screen at this time?
/**
* 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