jQuery filter to detect if a element or part of it is appearing in the window (viewport)
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.expr[':'].appearing = function(elem) { | |
var $window = $(window), | |
windowViewTop = $window.scrollTop(), | |
windowViewBottom = windowViewTop + $window.height(), | |
elemTop = $(elem).offset().top, | |
elemBottom = elemTop + $(elem).height(), | |
isAppearingFully = ((elemTop >= windowViewTop) && (elemBottom <= windowViewBottom)), | |
isAppearingBottom = ((windowViewTop > elemTop) && (windowViewTop < elemBottom)), | |
isAppearingTop = ((windowViewBottom > elemTop) && (windowViewTop < elemBottom)); | |
return isAppearingFully || isAppearingBottom || isAppearingTop; | |
}; |
Nice improvement.
Updated :D
There's an error in line 9, it should be
#9: isAppearingFully = ((elemTop >= windowViewTop) && (elemBottom <= windowViewBottom)),
I've fixed it. Thanks @jhuesos
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little improvement: