Skip to content

Instantly share code, notes, and snippets.

@heytricia
Forked from timkeller/application.js
Created December 19, 2016 20:07
Show Gist options
  • Save heytricia/3aea374b7863945db32c5e66d2a546aa to your computer and use it in GitHub Desktop.
Save heytricia/3aea374b7863945db32c5e66d2a546aa to your computer and use it in GitHub Desktop.
JS to make .subnav sticky
!function ($) {
$(function(){
// fix sub nav on scroll
var $win = $(window)
, $nav = $('.subnav')
, navTop = $('.subnav').length && $('.subnav').offset().top - 40
, isFixed = 0
processScroll()
$win.on('scroll', processScroll)
function processScroll() {
var i, scrollTop = $win.scrollTop()
if (scrollTop >= navTop && !isFixed) {
isFixed = 1
$nav.addClass('subnav-fixed')
} else if (scrollTop <= navTop && isFixed) {
isFixed = 0
$nav.removeClass('subnav-fixed')
}
}
})
}(window.jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment