Created
December 17, 2015 03:27
-
-
Save verticalgrain/4dd687f64e1d183f4cd9 to your computer and use it in GitHub Desktop.
Throttle scroll based interaction trigger
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
| function throttle(callback, limit) { | |
| var wait = false; | |
| return function () { | |
| if (!wait) { | |
| callback.call(); | |
| wait = true; | |
| setTimeout(function () { | |
| wait = false; | |
| }, limit); | |
| } | |
| }; | |
| }, | |
| function statisticsAnimation() { | |
| if ( !$('.statistics').hasClass('is-animated')) { | |
| var scrollPos = $(document).scrollTop(), | |
| windowHeight = $(window).height(), | |
| windowBottomPos = scrollPos + windowHeight - 100, | |
| statsOffset = $('.statistics').offset(), | |
| statsPos = statsOffset.top; | |
| if (windowBottomPos > statsPos) { | |
| console.log('it happened at: ' + windowBottomPos); | |
| $('.statistics').addClass('is-animated'); | |
| } | |
| } | |
| }, | |
| $(window).load(function() { | |
| statisticsAnimation(); | |
| window.addEventListener("scroll", throttle(statisticsAnimation, 200 )); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment