Last active
November 30, 2018 20:53
-
-
Save christowiz/653b76d9e33868e9f0b37a3314762ff2 to your computer and use it in GitHub Desktop.
Revisions
-
christowiz revised this gist
Nov 30, 2018 . 1 changed file with 4 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -32,12 +32,10 @@ // } function scroller(config) { if (!config.callback) throw Error('Scroller missing callback function'); const element = config.element || window; const callback = config.callback; let ticking = false; element.addEventListener('scroll', function(e) { @@ -59,4 +57,4 @@ function scroller(config) { }; } export default scroller; -
christowiz revised this gist
Nov 29, 2018 . 1 changed file with 11 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -31,21 +31,26 @@ // if ( topPosition <= -20 && !this.state.hideLabel ) this.hideLabel( true ); // } function scroller(config) { const element = config.element || window; const callback = config.callback || function() { console.log('noop'); }; let ticking = false; element.addEventListener('scroll', function(e) { if (!ticking) { window.requestAnimationFrame(function() { callback(e); ticking = false; }); ticking = true; } }); const remove = function() { element.removeEventListener('scroll', callback); }; @@ -54,4 +59,4 @@ function scroller({ element = window, cb }) { }; } module.exports = scroller; -
christowiz revised this gist
Nov 29, 2018 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -41,9 +41,8 @@ function scroller({ element = window, cb }) { callback(e); ticking = false; }); ticking = true; } }); const remove = () => { @@ -55,4 +54,4 @@ function scroller({ element = window, cb }) { }; } export default scroller; -
christowiz created this gist
Nov 29, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ 'use strict'; // * Scroll optimization using window.requestAnimationFrame // * // * https://developer.mozilla.org/en-US/docs/Web/Events/scroll // * // * @module scroller // * @param {Object} element DOM element to attach listener to // * @param {Func} cb Callback method to return event object // * @returns {Func} Method to remove event listener from element // // Example: // // componentWillUnmount(){ // this.scroller.remove(); // } // // onEnter = () => { // this.entered = true; // this.content = document.querySelector( '.modal-content' ); // this.scroller = scroller({ // element: document.querySelector( '.aria-modal' ), // cb: this.handleScroll // }); // } // // handleScroll = ( e ) => { // const topPosition = this.content.getBoundingClientRect().top; // // if ( topPosition > -20 && this.state.hideLabel ) this.hideLabel( false ); // if ( topPosition <= -20 && !this.state.hideLabel ) this.hideLabel( true ); // } function scroller({ element = window, cb }) { let ticking = false; const callback = cb; element.addEventListener('scroll', e => { if (!ticking) { window.requestAnimationFrame(() => { callback(e); ticking = false; }); } ticking = true; }); const remove = () => { element.removeEventListener('scroll', callback); }; return { remove, }; } export default scroller;