Created
April 20, 2016 17:11
-
-
Save haliphax/a772a2f6d9760efb4f37eb6c2cba0710 to your computer and use it in GitHub Desktop.
jQuery :inview and :reached psuedo-selectors
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
// 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