Skip to content

Instantly share code, notes, and snippets.

@hugofabricio
Created November 11, 2017 16:18
Show Gist options
  • Save hugofabricio/bd44e7dc360fc3b59d86faf74e600ae2 to your computer and use it in GitHub Desktop.
Save hugofabricio/bd44e7dc360fc3b59d86faf74e600ae2 to your computer and use it in GitHub Desktop.
class Sticky {
constructor() {
console.log('>>> Sticky constructor');
const that = this;
let scrollTop = $(window).scrollTop();
$('.js-sticky').each(function() {
let el = $(this),
offset = el.data('offset') || $(window).height() - 100;
that.stickyElement(scrollTop, el, offset);
$(window).scroll(function() {
scrollTop = $(window).scrollTop();
that.stickyElement(scrollTop, el, offset);
});
});
}
stickyElement(scrollTop, element, offset) {
if (scrollTop >= offset) {
element.addClass('-sticky');
} else {
element.removeClass('-sticky');
}
}
}
export default Sticky;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment