Created
October 1, 2018 16:07
-
-
Save khripunovpp/3e7f985265f2aba17081109eb88b401b to your computer and use it in GitHub Desktop.
scrollDetection
This file contains hidden or 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.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