Skip to content

Instantly share code, notes, and snippets.

@khripunovpp
Created October 1, 2018 16:07
Show Gist options
  • Save khripunovpp/3e7f985265f2aba17081109eb88b401b to your computer and use it in GitHub Desktop.
Save khripunovpp/3e7f985265f2aba17081109eb88b401b to your computer and use it in GitHub Desktop.
scrollDetection
$.fn.isOnScreen = function(shift) {
if (!shift) {
shift = 0;
}
var viewport = {};
viewport.top = $(window).scrollTop();
viewport.bottom = viewport.top + $(window).height();
var bounds = {};
bounds.top = this.offset().top + shift;
bounds.bottom = bounds.top + this.outerHeight() - shift;
return ((bounds.top <= viewport.bottom) && (bounds.bottom >= viewport.top));
};
var scrollDetection = function() {
$('.js-scroll-detection').each(function() {
var that = this;
var shift = $(that).data('scroll-shift') || 20;
if ($(that).isOnScreen(shift)) {
$(that).addClass('js-isonscreen');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment