Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import {noop} from '../func/noop';
import {Raf} from '../raf/raf';
import {DomWatcher} from './dom-watcher';
export interface ScrollRenderFixConfig {
/**
* Callback run immediately before the document is manually scrolled.
* Run during the RAF postWrite step.
*/
beforeScrollCallback?: () => void;
/**
* Callback run immediately after the document is manually scrolled.
* Run during the RAF postWrite step.
*/
afterScrollCallback?: () => void;
}
/**
* What this class does is, it eats the window.wheel event
* and eats the scroll. It allows the rendering to catch up and then
* once it is done, it reapplies the scroll to the document.
*
* While this sounds counter-intuitive, it allows rendering to catchup
* and can smooth animations.
*
* In short, if you have scroll tied animations or intense animations,
* this can help fix the issue.
*
*
* Thanks to Angus and Eric for this tip.
*
* Since this issue is chrome specific, you might want scope it to only chrome.
* Usage:
*
* ```
* if (is.chrome()) {
* new ScrollRenderFix();
* }
* ```
*
* To take full advantage, use toolbox mutate or degu.read / writes.
* ```
*
* const raf = new Raf();
*
* raf.read(()=> {
* // do some reading
* })
*
* raf.writing(()=> {
* // do some writing
* })
*
* ```
*
*
*
*/
export class ScrollRenderFix {
private raf: Raf;
private currentY = 0;
private targetY = 0;
private domWatcher: DomWatcher;
/**
* Callback run immediately before the document is manually scrolled.
* Run during the RAF postWrite step.
* @private
*/
private readonly beforeScrollCallback: () => void;
/**
* Callback run immediately after the document is manually scrolled.
* Run during the RAF postWrite step.
* @private
*/
private readonly afterScrollCallback: () => void;
constructor(config: ScrollRenderFixConfig = {}) {
this.raf = new Raf();
this.beforeScrollCallback = config.beforeScrollCallback || noop;
this.afterScrollCallback = config.afterScrollCallback || noop;
this.domWatcher = new DomWatcher();
this.domWatcher.add({
element: document,
on: 'wheel',
eventOptions: {passive: false, capture: true},
callback: this.wheelHandler.bind(this),
});
}
private getScrollElement(): Element {
return document.scrollingElement || document.documentElement;
}
private wheelHandler(e: WheelEvent) {
e.preventDefault();
this.raf.read(() => {
this.targetY = this.getScrollElement().scrollTop + e.deltaY;
this.raf.postWrite(() => {
this.beforeScrollCallback();
if (this.currentY !== this.targetY) {
this.getScrollElement().scrollTop = this.targetY;
this.currentY = this.targetY;
}
this.afterScrollCallback();
});
});
}
public dispose() {
this.domWatcher && this.domWatcher.dispose();
this.raf && this.raf.dispose();
}
}
|