Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ScrollManager {
- constructor() {
- this.subs = {};
- this._isTicking = false;
- this._scrollAmt = this._getPageScrollAmt();
- }
- _getPageScrollAmt() {
- return window.pageYOffset || document.documentElement.scrollTop;
- }
- addListeners() {
- window.addEventListener('scroll', () => this._requestTick());
- }
- removeListeners() {
- window.removeEventListener('scroll', () => this._requestTick());
- }
- publish(scrollPosition) {
- for (const key in this.subs) {
- if (this.subs.hasOwnProperty(key)) {
- this.subs[key](scrollPosition);
- }
- }
- }
- subscribe(key, fn) {
- this.subs[key] = fn;
- }
- unsubscribe(key) {
- delete this.subs[key];
- }
- _requestTick() {
- if (!this._isTicking) {
- requestAnimationFrame(() => this._onScroll());
- this._isTicking = true;
- }
- }
- _onScroll() {
- this._scrollAmt = this._getPageScrollAmt();
- this.publish(this._scrollAmt);
- this._isTicking = false;
- }
- }
- export default ScrollManager;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement