Created
June 12, 2016 18:26
-
-
Save m3g4p0p/371f9405c3d868816346deafd3c6ca21 to your computer and use it in GitHub Desktop.
jQuery extension to reduce the set of matched elements to those being (vertically) in the 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
$.fn.visible = function() { | |
'use strict'; | |
var $window = $(window), | |
windowScrollTop = $window.scrollTop(), | |
windowHeight = $window.height(); | |
return this.filter(function() { | |
var $this = $(this), | |
thisOffsetTop = $this.offset().top, | |
thisHeight = $this.height(); | |
return thisOffsetTop < | |
windowScrollTop + | |
windowHeight && | |
thisOffsetTop + | |
thisHeight > | |
windowScrollTop; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment