Skip to content

Instantly share code, notes, and snippets.

@haliphax
Created April 20, 2016 17:11
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 haliphax/a772a2f6d9760efb4f37eb6c2cba0710 to your computer and use it in GitHub Desktop.
Save haliphax/a772a2f6d9760efb4f37eb6c2cba0710 to your computer and use it in GitHub Desktop.
jQuery :inview and :reached psuedo-selectors
// jQuery pseudo-selectors to figure out if an element is on-screen
$.extend($.expr[':'], {
// determines if the element is within the viewport
'inviewport': function (el) {
var $el = $(el)
, h = $el.outerHeight()
, t = $el.offset().top
, b = t + h
, s = $(document).scrollTop()
, w = $(window).height()
, W = s + w
;
return ((t >= s && t <= W) || (b >= s && b <= W));
}
// determines if we have reached or passed the top of the element
, 'reached': function (el) {
var $el = $(el)
, t = $el.offset().top
, s = $(document).scrollTop()
;
return (s >= t);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment