Skip to content

Instantly share code, notes, and snippets.

@m3g4p0p
Created June 12, 2016 18:26
Show Gist options
  • Save m3g4p0p/371f9405c3d868816346deafd3c6ca21 to your computer and use it in GitHub Desktop.
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
$.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