Skip to content

Instantly share code, notes, and snippets.

@rhyzx
Last active October 29, 2020 17:50
Show Gist options
  • Save rhyzx/9613192 to your computer and use it in GitHub Desktop.
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/
.headroom {
position: fixed;
top: 0;
-webkit-transition: top 0.15s;
transition: top 0.15s;
}
.headroom-hidden {
top: -60px;
}
$(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
}
}
})
})
})
@MJMultari
Copy link

Thank you so much for this. Trying to get headroom.js to work was such a pain!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment