Last active
December 15, 2015 22:29
-
-
Save MikeMcChillin/5333753 to your computer and use it in GitHub Desktop.
Set up scroll events. There's also this: http://codepen.io/MikeMcChillin/pen/lDcwq which uses Ben Alman's jquery throttle.
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
/** | |
* scrollEvents.js | |
* | |
* Licensed under the MIT license. | |
* http://www.opensource.org/licenses/mit-license.php | |
* | |
*/ | |
// http://tympanus.net/codrops/2013/06/06/on-scroll-animated-header/ | |
var scrollEvents = (function() { | |
var docElem = document.documentElement, | |
header = document.querySelector( '.cbp-af-header' ), | |
didScroll = false, | |
changeHeaderOn = 300, | |
debounce = 250; | |
function init() { | |
window.addEventListener( 'scroll', function( event ) { | |
if( !didScroll ) { | |
didScroll = true; | |
setTimeout( scrollPage, debounce ); | |
} | |
}, false ); | |
} | |
function scrollPage() { | |
var sy = scrollY(); | |
console.log("scrollY(): " + scrollY()); | |
if ( sy >= changeHeaderOn ) { | |
classie.add( header, 'cbp-af-header-shrink' ); | |
} | |
else { | |
classie.remove( header, 'cbp-af-header-shrink' ); | |
} | |
didScroll = false; | |
} | |
// function scrollY() { | |
// return window.pageYOffset || docElem.scrollTop; | |
// } | |
function scrollY(){ | |
if ( typeof pageYOffset != 'undefined') { | |
//most browsers except IE before #9 | |
return pageYOffset; | |
} | |
else { | |
var B = document.body; //IE 'quirks' | |
var D = document.documentElement; //IE with doctype | |
D = ( D.clientHeight ) ? D : B; | |
return D.scrollTop; | |
} | |
} | |
init(); | |
})(); |
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
######################### | |
# Scroll events | |
######################### | |
$(window).scroll -> | |
scroll = $(window).scrollTop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment