Skip to content

Instantly share code, notes, and snippets.

@danielt69
Last active June 4, 2018 16:25
Show Gist options
  • Select an option

  • Save danielt69/90fa67a616537ce929074f75384c5fe0 to your computer and use it in GitHub Desktop.

Select an option

Save danielt69/90fa67a616537ce929074f75384c5fe0 to your computer and use it in GitHub Desktop.
check to see if element is scrolled out of the screen and act on it
$.fn.isBottomInScreen = function (outFn, inFn) {
outFn = outFn || $.noop();
inFn = inFn || $.noop();
var $self = $(this);
var inFlag = true;
$(window).on('scroll', function () {
requestAnimationFrame(function () {
if (($self.offset().top + $self.height()) < $(window).scrollTop()) { // element is out of screen
if (inFlag) {
$self.addClass('out').removeClass('in').trigger('out');
outFn.apply(this, arguments);
inFlag = false;
}
} else {
if (!inFlag) {
$self.addClass('in').removeClass('out').trigger('in');
inFn.apply(this, arguments);
inFlag = true;
}
}
});
}).trigger('scroll');
};
$('.mydiv').isBottomInScreen(
function () { // out
$('.ab_test').attr('data-visible', 'false');
},
function () { // in
$('.ab_test').attr('data-visible', 'true');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment