Skip to content

Instantly share code, notes, and snippets.

@mthchz
Created May 22, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mthchz/8eef5c38bc59f920aefa1dcb410cf455 to your computer and use it in GitHub Desktop.
Save mthchz/8eef5c38bc59f920aefa1dcb410cf455 to your computer and use it in GitHub Desktop.
JS element is visible ?
/* Element is visible ?
*******************************************************************************/
$.fn.isVisible = function(partial) {
if( typeof $(this).offset() === 'undefined' ) return false;
var $t = $(this),
$w = $(window),
viewTop = $w.scrollTop(),
viewBottom = viewTop + $w.height() - 100,
_top = $t.offset().top,
_bottom = _top + $t.height(),
compareTop = partial === true ? _bottom : _top,
compareBottom = partial === true ? _top : _bottom;
return ((compareBottom <= viewBottom) && (compareTop >= viewTop));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment