Skip to content

Instantly share code, notes, and snippets.

@originell
Last active August 29, 2015 14:20
Show Gist options
  • Save originell/7a666ee783b3cf21d733 to your computer and use it in GitHub Desktop.
Save originell/7a666ee783b3cf21d733 to your computer and use it in GitHub Desktop.
Example: detect scroll position and change navi style
// Navigationbar to be changed (cached here for performance, always grabbing
// it new in the scroll-callback is bad for performance)
var $navi = $('.myNav')
function changeNavStyle() {
var top = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
if (top > 200) {
$navi.addClass('.myNewStyle-Yo')
} else {
$navi.removeClass('.myNewStyle-Yo')
}
}
// Call this once when the page loads, because the browser might restore the user
// to a scroll position where we need to change styles!
changeNavStyle()
// Actual scroll check
$(window).scroll(changeNavStyle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment