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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | import * as dom from '../dom/dom';
import {DomWatcher} from '../dom/dom-watcher';
import {
type ElementVisibilityObject,
elementVisibility,
} from '../dom/element-visibility';
import * as mathf from '../mathf/mathf';
export interface ScrollPointsConfig {
triggerElement: HTMLElement;
// How much of the trigger element should be visible to trigger.
triggerThreshold: number;
// The direction in which the trigger should happen. 1 means this would only
// trigger when going down. -1 would trigger only when going up and 0 would
// trigger for both up and down.
triggerDirection: number;
targetElement: HTMLElement;
// The amount of scroll that should happen for every 1 second duration.
// This is in effect, the "speed" of scroll but rather sets the duration
// to be a factor of distance (meaning the scroll is always a constant speed
// independent of distance).
scrollDistanceEvery1Second?: number;
// When the trigger happens, what position - location the scroll should go to.
// relative to the top of the window.
targetElementOffsetFromTopOfWindow: Function;
// Whether to use a native smooth scroll.
// Native scroll will work better but the speed of travel
// can't be controlled and may differ based on the distance
// of travel. Turning this on, makes it run it via a JS
// based window scroll in which the travel speed factors
// the distance it needs to travel (per scrollDistanceEvery1Second)
forceNativeScroll: boolean;
}
/**
A class that scrolls the window to a targetElement when a given
trigger element becomes visible on the page.
Given a trigger and target, here is an example that says,
when 20% of the trigger element is visible, then scroll to the
top of the target element.
The trigger and target can be the same element.
```
<div trigger>
<div target></div>
</div>
new ScrollPoints({
triggerElement: triggerElement,
triggerThreshold: 0.2,
triggerDirection: -1
targetElement: targetElement,
targetElementOffsetFromTopOfWindow: (trigger:HTMLElement, targetElement:HTMLElement)=> {
return 0;
}
})
```
*/
export class ScrollPoints {
private config: ScrollPointsConfig;
private ev: ElementVisibilityObject;
private domWatcher: DomWatcher;
private y = 0;
private prevY = 0;
private scrollDirection = 0;
private scrolling = false;
private cancelScroll: (() => void) | null = null;
constructor(config: ScrollPointsConfig) {
this.config = config;
this.domWatcher = new DomWatcher();
this.domWatcher.add({
element: window,
on: 'scroll',
callback: this.onWindowScroll.bind(this),
eventOptions: {passive: true},
});
this.onWindowScroll();
this.ev = elementVisibility.inview(
this.config.triggerElement,
{threshold: this.config.triggerThreshold || 0},
(element: HTMLElement, changes: IntersectionObserverEntry) => {
if (changes.isIntersecting) {
this.engage();
} else {
this.disengage();
}
}
);
}
private getOffset() {
return this.config.targetElementOffsetFromTopOfWindow
? this.config.targetElementOffsetFromTopOfWindow()
: 0;
}
private onWindowScroll() {
this.prevY = this.y;
this.y = window.scrollY;
this.scrollDirection = mathf.direction(this.prevY, this.y);
}
private engage(): void {
if (this.scrolling) {
return;
}
// Engage only when scroll directions match with config specification.
if (this.config.triggerDirection === -1 && this.scrollDirection !== -1) {
return;
}
if (this.config.triggerDirection === 1 && this.scrollDirection !== 1) {
return;
}
this.config.targetElement.classList.add('scroll-point-engaged');
// Native scroll.
if (this.config.forceNativeScroll) {
window.scrollTo({
top: dom.getScrollTop(this.config.targetElement) + this.getOffset(),
left: 0,
behavior: 'smooth',
});
} else {
const top =
dom.getScrollTop(this.config.targetElement) + this.getOffset();
// Calculate the duration. The duration factors in the current distance.
const distance = Math.abs(window.scrollY - top);
// 1 second for base distance.
const base = this.config.scrollDistanceEvery1Second || 1000;
const duration = (distance / base) * 1000;
this.cancelScroll && this.cancelScroll();
this.scrolling = true;
this.cancelScroll = dom.smoothScrollTo(top, duration, () => {
this.scrolling = false;
this.cancelScroll = null;
});
}
}
private disengage(): void {
this.config.targetElement.classList.remove('scroll-point-engaged');
}
public dispose(): void {
this.cancelScroll && this.cancelScroll();
this.cancelScroll = null;
this.ev.dispose();
this.domWatcher.dispose();
}
}
|