Last active
June 4, 2018 16:25
-
-
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
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.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