Last active
October 29, 2020 17:50
-
-
Save rhyzx/9613192 to your computer and use it in GitHub Desktop.
jQuery and minimal version of headroom.js http://wicky.nillia.ms/headroom.js/ demo http://jsfiddle.net/JNAQD/1/
This file contains 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
.headroom { | |
position: fixed; | |
top: 0; | |
-webkit-transition: top 0.15s; | |
transition: top 0.15s; | |
} | |
.headroom-hidden { | |
top: -60px; | |
} |
This file contains 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
$(function () { | |
$('.headroom').each(function () { | |
var $win = $(window) | |
, $self = $(this) | |
, isHidden = false | |
, lastScrollTop = 0 | |
$win.on('scroll', function () { | |
var scrollTop = $win.scrollTop() | |
var offset = scrollTop - lastScrollTop | |
lastScrollTop = scrollTop | |
// min-offset, min-scroll-top | |
if (offset > 10 && scrollTop > 200 ) { | |
if (!isHidden) { | |
$self.addClass('headroom-hidden') | |
isHidden = true | |
} | |
} else if (offset < -10 || scrollTop < 200) { | |
if (isHidden) { | |
$self.removeClass('headroom-hidden') | |
isHidden = false | |
} | |
} | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for this. Trying to get headroom.js to work was such a pain!