Created
February 14, 2019 11:55
-
-
Save marcusandre/13dd06b884206fa2bc9b21670403929d to your computer and use it in GitHub Desktop.
simple sticky scrolling header
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
/* 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