Last active
August 29, 2015 14:27
-
-
Save Razenbull/3e938af8e639bc66b7af to your computer and use it in GitHub Desktop.
sticky header with animation
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
$transition-duration: .3s !default; | |
$transition-timing-function: ease !default; | |
$sticky-limit: 80px; | |
.sticky { | |
@include transition-duration($transition-duration); | |
@include transition-property(top); | |
@include transition-timing-function($transition-timing-function); | |
position: fixed; | |
top: -$sticky-limit; | |
left: 0; | |
right: 0; | |
} | |
.sticky-top { | |
top: 0; | |
} |
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 stickyTimeout = false; | |
// check if scrollTop > limit and if so stick header to top | |
function stickHeader() { | |
var scrollTop = $(window).scrollTop(), | |
$header = $('.ultimate-header'), | |
limit = $('.ultimate-header').hasClass('sticky-top') ? 0 : $('.navbar-nav').height(); | |
if(scrollTop > limit) { | |
if(!stickyTimeout){ | |
$header.addClass('sticky'); | |
stickyTimeout = window.setTimeout(function() { | |
$header.addClass('sticky-top'); | |
}, 300); | |
} | |
} else { | |
if(stickyTimeout) window.clearTimeout(stickyTimeout); | |
stickyTimeout=false; | |
$header.removeClass('sticky-top'); | |
$header.removeClass('sticky'); | |
} | |
} | |
stickHeader(); | |
// check on window scroll | |
$(window).off('scroll').scroll(function() { | |
stickHeader(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment