Created
November 11, 2017 16:18
-
-
Save hugofabricio/bd44e7dc360fc3b59d86faf74e600ae2 to your computer and use it in GitHub Desktop.
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
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