Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leochocolat/9bf041e305e5414d74103218740595e6 to your computer and use it in GitHub Desktop.
Save leochocolat/9bf041e305e5414d74103218740595e6 to your computer and use it in GitHub Desktop.
import EventDispatcher from '../events/EventDispatcher';
import bindAll from '../utils/bindAll';
import DeviceUtils from '../utils/DeviceUtils';
import VirtualScroll from 'virtual-scroll';
class SnapScrollManager extends EventDispatcher {
constructor(options = {}) {
super(options);
bindAll(
this,
'_virtualScrollHandler',
'_virtualScrollEndHandler'
);
this._allowSnap = true;
this._allowWheel = true;
this._setup();
}
removeEventListener() {
this._virtualScroll.off(this._virtualScrollHandler);
}
_setup() {
this._setupVirtualScroll();
this._setupEventListeners();
}
_setupVirtualScroll() {
const options = {
limitInertia: true
};
this._virtualScroll = new VirtualScroll(options)
}
_setupEventListeners() {
this._virtualScroll.on(this._virtualScrollHandler);
}
_virtualScrollHandler(e) {
this._wheelDirection = e.deltaY <= 0 ? 'down' : 'up';
e.direction = this._wheelDirection;
if (Math.abs(e.deltaY) > 4) {
clearTimeout(this._wheelTimeout);
this._wheelTimeout = setTimeout(() => {
this._virtualScrollEndHandler(e);
}, 100);
if (!this._allowSnap) return;
this._allowSnap = false;
this.dispatchEvent('wheel:snap', e);
console.log(e);
//TODO: Try to cancel scroll event when snap is called
}
}
_virtualScrollEndHandler(e) {
this._allowSnap = true;
this.dispatchEvent('wheel:snap:end', e);
}
}
export default new SnapScrollManager();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment