Skip to content

Instantly share code, notes, and snippets.

@marcusandre
Created February 14, 2019 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusandre/13dd06b884206fa2bc9b21670403929d to your computer and use it in GitHub Desktop.
Save marcusandre/13dd06b884206fa2bc9b21670403929d to your computer and use it in GitHub Desktop.
simple sticky scrolling header
/* global $ */
import throttle from 'raf-throttle'
export default class Header {
constructor (el) {
this.el = el
this.hiddenClass = 'hidden'
}
stick () {
if (this.el.hasClass('Header--Sticky')) {
$('html').addClass('em-sticky')
}
$(window).on(
'scroll',
throttle(() => {
this.onScroll()
})
)
}
onScroll () {
const y = window.scrollY
const h = this.el.height()
const hasClass = this.el.hasClass(this.hiddenClass)
if (y <= h && hasClass) {
this.el.removeClass(this.hiddenClass)
}
if (y > h) {
if (y > this._y && !hasClass) {
this.el.addClass(this.hiddenClass)
}
if (y < this._y && hasClass) {
this.el.removeClass(this.hiddenClass)
}
}
this._y = y
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment