Last active
August 19, 2016 18:08
-
-
Save evanre/064554519cd7894850e0bba680cd9de9 to your computer and use it in GitHub Desktop.
Show sticky header when start to scrollup
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
<div id="page" class="site"> | |
<header id="masthead" class="site-header" role="banner"></header> | |
</div> |
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 didScroll, | |
lastScrollTop = 0, | |
scrollDelta = 20, | |
header = $('#masthead'), | |
headerHeight = header.outerHeight(), | |
scrollClass = 'is-scrolling-down' | |
$(window).scroll(function(event){ | |
didScroll = true; | |
}); | |
setInterval(function() { | |
if (didScroll && $(window).width() <= '767') { | |
hasScrolled(); | |
didScroll = false; | |
} | |
}, 500); | |
function hasScrolled() { | |
var st = $(this).scrollTop(); | |
// Make sure they scroll more than delta | |
if (Math.abs(lastScrollTop - st) <= scrollDelta) | |
return; | |
// If they scrolled down and are past the navbar, add class scrollClass. | |
// This is necessary so you never see what is "behind" the navbar. | |
if (st > lastScrollTop && st > headerHeight) { | |
// Scroll Down | |
if (!header.hasClass(scrollClass)) { | |
header.addClass(scrollClass); | |
} | |
} else { | |
// Scroll Up | |
if (st + $(window).height() < $(document).height()) { | |
if (header.hasClass(scrollClass)) { | |
header.removeClass(scrollClass); | |
} | |
} | |
} | |
lastScrollTop = st; | |
} |
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
// Sticky header | |
#page { | |
padding-top: 110px; // height of header | |
@extend %transition; | |
} | |
.site-header { | |
position: fixed; | |
top: 0; | |
height: 110px; | |
transition: transform 0.2s ease-in-out; | |
z-index: 500; | |
transform: translateY(0); | |
} | |
.is-scrolling-down { | |
transform: translateY(-100%); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment