Created
March 20, 2017 10:41
-
-
Save ben182/309cc4e208ab6c5ed5c77f8cc20d6c87 to your computer and use it in GitHub Desktop.
Plain Javascript on scroll event
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
| var getScroll = function() { | |
| if (window.pageYOffset != undefined) { | |
| return [pageXOffset, pageYOffset]; | |
| } else { | |
| var sx, sy, d = document, | |
| r = d.documentElement, | |
| b = d.body; | |
| sx = r.scrollLeft || b.scrollLeft || 0; | |
| sy = r.scrollTop || b.scrollTop || 0; | |
| return [sx, sy]; | |
| } | |
| } | |
| window.onscroll = function(e) { | |
| var iHScroll = getScroll()[1]; | |
| if (iHScroll > 20) { | |
| $('nav').addClass('active'); //example | |
| } else { | |
| $('nav').removeClass('active'); //example | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment