Skip to content

Instantly share code, notes, and snippets.

@Tusko
Created March 19, 2017 20:43
Show Gist options
  • Select an option

  • Save Tusko/2db7fa2a98b9402bb69ff5c62559a54c to your computer and use it in GitHub Desktop.

Select an option

Save Tusko/2db7fa2a98b9402bb69ff5c62559a54c to your computer and use it in GitHub Desktop.
Check if element is visible (in viewport(
(function($) {
$.fn.visible = function(partial, hidden) {
var $t = $(this).eq(0),
t = $t.get(0),
$w = $(window),
viewTop = $w.scrollTop(),
viewBottom = viewTop + $w.height(),
_top = $t.offset().top,
_bottom = _top + $t.height(),
compareTop = partial === true ? _bottom : _top,
compareBottom = partial === true ? _top : _bottom,
clientSize = hidden === true ? t.offsetWidth * t.offsetHeight : true;
return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop));
};
})(jQuery);
/*
if( $('.el').visible(true) ) {
//your code here
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment