vevet 2.3.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cdn/index.js +1 -1
- package/build/cjs/components/scroll/section/ScrollSectionProgress.js +261 -0
- package/build/cjs/index.js +5 -1
- package/build/cjs/utils/scroll/getScrollValues.js +20 -0
- package/build/cjs/utils/scroll/index.js +12 -0
- package/build/cjs/utils/scroll/scrollTo.js +41 -0
- package/build/cjs/utils/scroll/scrollToElement.js +39 -0
- package/build/cjs/utils/scroll/to.js +41 -0
- package/build/es/components/scroll/section/ScrollSectionProgress.js +181 -0
- package/build/es/index.js +4 -1
- package/build/es/utils/scroll/getScrollValues.js +16 -0
- package/build/es/utils/scroll/index.js +4 -0
- package/build/es/utils/scroll/scrollTo.js +34 -0
- package/build/es/utils/scroll/scrollToElement.js +32 -0
- package/build/es/utils/scroll/to.js +34 -0
- package/build/types/components/scroll/section/ScrollSectionProgress.d.ts +131 -0
- package/build/types/components/scroll/section/ScrollSectionProgress.d.ts.map +1 -0
- package/build/types/components/scroll/types.d.ts +2 -5
- package/build/types/components/scroll/types.d.ts.map +1 -1
- package/build/types/index.d.ts +4 -1
- package/build/types/index.d.ts.map +1 -1
- package/build/types/utils/scroll/getScrollValues.d.ts +9 -0
- package/build/types/utils/scroll/getScrollValues.d.ts.map +1 -0
- package/build/types/utils/scroll/index.d.ts +5 -0
- package/build/types/utils/scroll/index.d.ts.map +1 -0
- package/build/types/utils/scroll/scrollTo.d.ts +28 -0
- package/build/types/utils/scroll/scrollTo.d.ts.map +1 -0
- package/build/types/utils/scroll/scrollToElement.d.ts +32 -0
- package/build/types/utils/scroll/scrollToElement.d.ts.map +1 -0
- package/build/types/utils/scroll/to.d.ts +28 -0
- package/build/types/utils/scroll/to.d.ts.map +1 -0
- package/build/types/utils/types/general.d.ts +6 -0
- package/build/types/utils/types/general.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/ts/components/scroll/section/ScrollSectionProgress.ts +344 -0
- package/src/ts/components/scroll/types.ts +3 -5
- package/src/ts/index.ts +4 -0
- package/src/ts/utils/scroll/getScrollValues.ts +20 -0
- package/src/ts/utils/scroll/index.ts +9 -0
- package/src/ts/utils/scroll/scrollTo.ts +65 -0
- package/src/ts/utils/scroll/scrollToElement.ts +74 -0
- package/src/ts/utils/types/general.ts +7 -0
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { selectOne } from 'vevet-dom';
|
|
2
|
+
import PCancelable from 'p-cancelable';
|
|
3
|
+
import { IRemovable } from '../../../utils/types/general';
|
|
4
|
+
import { RequiredModuleProp } from '../../../utils/types/utility';
|
|
5
|
+
import onScroll from '../../../utils/listeners/onScroll';
|
|
6
|
+
import { Component, NComponent } from '../../../base/Component';
|
|
7
|
+
import { NViewport } from '../../../app/events/Viewport';
|
|
8
|
+
import { SmoothScroll } from '../smooth-scroll/SmoothScroll';
|
|
9
|
+
import clampScope from '../../../utils/math/clampScope';
|
|
10
|
+
import getScrollValues from '../../../utils/scroll/getScrollValues';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export namespace NScrollSectionProgress {
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Static properties
|
|
18
|
+
*/
|
|
19
|
+
export interface StaticProp extends NComponent.StaticProp {
|
|
20
|
+
/**
|
|
21
|
+
* The scrollable container
|
|
22
|
+
* @default window
|
|
23
|
+
*/
|
|
24
|
+
container?: string | Element | SmoothScroll | Window;
|
|
25
|
+
/**
|
|
26
|
+
* The scrollable element
|
|
27
|
+
*/
|
|
28
|
+
section: string | Element;
|
|
29
|
+
/**
|
|
30
|
+
* Update sizes on resize
|
|
31
|
+
* @default false
|
|
32
|
+
*/
|
|
33
|
+
viewportTarget?: keyof NViewport.CallbacksTypes;
|
|
34
|
+
/**
|
|
35
|
+
* @default 0
|
|
36
|
+
*/
|
|
37
|
+
resizeTimeout?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Changeable properties
|
|
42
|
+
*/
|
|
43
|
+
export interface ChangeableProp extends NComponent.ChangeableProp { }
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Available callbacks
|
|
47
|
+
*/
|
|
48
|
+
export interface CallbacksTypes extends NComponent.CallbacksTypes {
|
|
49
|
+
'render': false;
|
|
50
|
+
'resize': false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Elements into viewport
|
|
59
|
+
*/
|
|
60
|
+
export class ScrollSectionProgress <
|
|
61
|
+
StaticProp extends NScrollSectionProgress.StaticProp
|
|
62
|
+
= NScrollSectionProgress.StaticProp,
|
|
63
|
+
ChangeableProp extends NScrollSectionProgress.ChangeableProp
|
|
64
|
+
= NScrollSectionProgress.ChangeableProp,
|
|
65
|
+
CallbacksTypes extends NScrollSectionProgress.CallbacksTypes
|
|
66
|
+
= NScrollSectionProgress.CallbacksTypes,
|
|
67
|
+
> extends Component <
|
|
68
|
+
StaticProp,
|
|
69
|
+
ChangeableProp,
|
|
70
|
+
CallbacksTypes
|
|
71
|
+
> {
|
|
72
|
+
protected _getDefaultProp <
|
|
73
|
+
T extends RequiredModuleProp<StaticProp & ChangeableProp>
|
|
74
|
+
> (): T {
|
|
75
|
+
return {
|
|
76
|
+
...super._getDefaultProp(),
|
|
77
|
+
container: window,
|
|
78
|
+
viewportTarget: '',
|
|
79
|
+
resizeTimeout: 0,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Scroll container
|
|
85
|
+
*/
|
|
86
|
+
protected _scrollContainer: Element | SmoothScroll | Window;
|
|
87
|
+
get scrollContainer () {
|
|
88
|
+
return this._scrollContainer;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Section element
|
|
93
|
+
*/
|
|
94
|
+
protected _section: Element;
|
|
95
|
+
get section () {
|
|
96
|
+
return this._section;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Scroll event
|
|
101
|
+
*/
|
|
102
|
+
protected _scrollEvent?: IRemovable;
|
|
103
|
+
/**
|
|
104
|
+
* Loaded event
|
|
105
|
+
*/
|
|
106
|
+
protected _loadedEvent?: PCancelable<any>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Scrolling scope
|
|
110
|
+
*/
|
|
111
|
+
protected _scopeScroll: [number, number];
|
|
112
|
+
get scopeScroll () {
|
|
113
|
+
return this._scopeScroll;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 'In' scope relative to the global progress
|
|
118
|
+
*/
|
|
119
|
+
protected _scopeIn: [number, number];
|
|
120
|
+
protected get scopeIn () {
|
|
121
|
+
return this._scopeIn;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 'Move' scope relative to the global progress
|
|
126
|
+
*/
|
|
127
|
+
protected _scopeMove: [number, number];
|
|
128
|
+
get scopeMove () {
|
|
129
|
+
return this._scopeMove;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 'Move' scope relative to the global progress
|
|
134
|
+
*/
|
|
135
|
+
protected _scopeOut: [number, number];
|
|
136
|
+
get scopeOut () {
|
|
137
|
+
return this._scopeOut;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
protected _progress: number;
|
|
141
|
+
/**
|
|
142
|
+
* Global progress
|
|
143
|
+
*/
|
|
144
|
+
get progress () {
|
|
145
|
+
return this._progress;
|
|
146
|
+
}
|
|
147
|
+
set progress (val) {
|
|
148
|
+
this._progress = val;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
constructor (
|
|
154
|
+
initialProp?: (StaticProp & ChangeableProp),
|
|
155
|
+
init = true,
|
|
156
|
+
) {
|
|
157
|
+
super(initialProp, false);
|
|
158
|
+
|
|
159
|
+
// get scroll container
|
|
160
|
+
if (typeof this.prop.container === 'string') {
|
|
161
|
+
this._scrollContainer = selectOne(this.prop.container) as Element;
|
|
162
|
+
} else {
|
|
163
|
+
this._scrollContainer = this.prop.container;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// get section element
|
|
167
|
+
if (typeof this.prop.section === 'string') {
|
|
168
|
+
this._section = selectOne(this.prop.section) as Element;
|
|
169
|
+
} else {
|
|
170
|
+
this._section = this.prop.section;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// set defaults
|
|
174
|
+
this._progress = -0.001;
|
|
175
|
+
this._scopeScroll = [0, 0];
|
|
176
|
+
this._scopeIn = [0, 0];
|
|
177
|
+
this._scopeMove = [0, 0];
|
|
178
|
+
this._scopeOut = [0, 0];
|
|
179
|
+
|
|
180
|
+
// initialize the class
|
|
181
|
+
if (init) {
|
|
182
|
+
this.init();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public init () {
|
|
187
|
+
super.init();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Set Module Events
|
|
191
|
+
protected _setEvents () {
|
|
192
|
+
super._setEvents();
|
|
193
|
+
|
|
194
|
+
// set resize events
|
|
195
|
+
this.addViewportCallback(this.prop.viewportTarget, () => {
|
|
196
|
+
this.resize();
|
|
197
|
+
}, {
|
|
198
|
+
timeout: this.prop.resizeTimeout,
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// resize on page loaded
|
|
202
|
+
this._loadedEvent = this._app.onPageLoaded();
|
|
203
|
+
this._loadedEvent.then(() => {
|
|
204
|
+
this.resize();
|
|
205
|
+
}).catch(() => {});
|
|
206
|
+
|
|
207
|
+
// set scroll events
|
|
208
|
+
this._scrollEvent = onScroll({
|
|
209
|
+
container: this.prop.container,
|
|
210
|
+
callback: this._handleScroll.bind(this),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
protected _onPropMutate () {
|
|
215
|
+
super._onPropMutate();
|
|
216
|
+
this.resize();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 'in' progress
|
|
223
|
+
*/
|
|
224
|
+
get progressIn () {
|
|
225
|
+
return clampScope(
|
|
226
|
+
this.progress,
|
|
227
|
+
this.scopeIn,
|
|
228
|
+
) || 0;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 'out' progress
|
|
233
|
+
*/
|
|
234
|
+
get progressOut () {
|
|
235
|
+
return clampScope(
|
|
236
|
+
this.progress,
|
|
237
|
+
this.scopeOut,
|
|
238
|
+
) || 0;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* 'move' progress
|
|
243
|
+
*/
|
|
244
|
+
get progressMove () {
|
|
245
|
+
return clampScope(
|
|
246
|
+
this.progress,
|
|
247
|
+
this.scopeMove,
|
|
248
|
+
) || 0;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Handle scroll event
|
|
255
|
+
*/
|
|
256
|
+
protected _handleScroll () {
|
|
257
|
+
// calculate scopes
|
|
258
|
+
const scrollData = getScrollValues(this.scrollContainer);
|
|
259
|
+
if (!scrollData) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
this._render(scrollData);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Resize the scene
|
|
269
|
+
*/
|
|
270
|
+
public resize () {
|
|
271
|
+
// calculate scopes
|
|
272
|
+
const scrollData = getScrollValues(this.scrollContainer);
|
|
273
|
+
if (!scrollData) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// get sizes
|
|
278
|
+
const bounding = this.section.getBoundingClientRect();
|
|
279
|
+
const vHeight = this._app.viewport.height;
|
|
280
|
+
|
|
281
|
+
// calculate scroll scope
|
|
282
|
+
const inStart = scrollData.scrollTop + bounding.top - vHeight;
|
|
283
|
+
const moveEnd = scrollData.scrollTop + bounding.top + bounding.height;
|
|
284
|
+
const scrollLine = moveEnd - inStart;
|
|
285
|
+
this._scopeScroll = [inStart, moveEnd];
|
|
286
|
+
|
|
287
|
+
// calculate scopes
|
|
288
|
+
this._scopeIn = [0, vHeight / scrollLine];
|
|
289
|
+
this._scopeOut = [1 - vHeight / scrollLine, 1];
|
|
290
|
+
this._scopeMove = [this._scopeIn[1], this._scopeOut[0]];
|
|
291
|
+
|
|
292
|
+
// launch callbacks
|
|
293
|
+
this.callbacks.tbt('resize', false);
|
|
294
|
+
|
|
295
|
+
// render the scene
|
|
296
|
+
this._render(scrollData, true);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Render the scene
|
|
301
|
+
*/
|
|
302
|
+
protected _render (
|
|
303
|
+
scrollData: ReturnType<typeof getScrollValues>,
|
|
304
|
+
force = false,
|
|
305
|
+
) {
|
|
306
|
+
const canRender = this._canRender(scrollData, force);
|
|
307
|
+
if (!canRender) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
this.callbacks.tbt('render', false);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Check if the section can be rendered
|
|
315
|
+
*/
|
|
316
|
+
protected _canRender (
|
|
317
|
+
scrollData: ReturnType<typeof getScrollValues>,
|
|
318
|
+
force = false,
|
|
319
|
+
) {
|
|
320
|
+
if (!scrollData) {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
const { scrollTop } = scrollData;
|
|
324
|
+
const prevProgress = this.progress;
|
|
325
|
+
|
|
326
|
+
const progress = clampScope(
|
|
327
|
+
scrollTop,
|
|
328
|
+
[this._scopeScroll[0], this._scopeScroll[1]],
|
|
329
|
+
) || 0;
|
|
330
|
+
this.progress = progress;
|
|
331
|
+
|
|
332
|
+
return force || (progress !== prevProgress);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Destroy the module
|
|
338
|
+
*/
|
|
339
|
+
protected _destroy () {
|
|
340
|
+
super._destroy();
|
|
341
|
+
this._scrollEvent?.remove();
|
|
342
|
+
this._loadedEvent?.cancel();
|
|
343
|
+
}
|
|
344
|
+
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
scrollTo(x: number, y: number): void;
|
|
5
|
-
scrollLeft: number;
|
|
1
|
+
import { ScrollLike } from '../../utils/types/general';
|
|
2
|
+
|
|
3
|
+
export interface ScrollableElement extends ScrollLike {
|
|
6
4
|
scrollWidth: number;
|
|
7
5
|
scrollHeight: number;
|
|
8
6
|
clientWidth: number;
|
package/src/ts/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as common from './utils/common';
|
|
2
2
|
import * as listeners from './utils/listeners';
|
|
3
3
|
import * as math from './utils/math';
|
|
4
|
+
import * as scroll from './utils/scroll';
|
|
4
5
|
|
|
5
6
|
import * as GeneralTypes from './utils/types/general';
|
|
6
7
|
|
|
@@ -39,6 +40,7 @@ import { ScrollEventsBase, NScrollEventsBase } from './components/scroll/scrolla
|
|
|
39
40
|
import { ScrollView, NScrollView } from './components/scroll/scrollable/ScrollView';
|
|
40
41
|
import { SmoothScrollKeyboardPlugin, NSmoothScrollKeyboardPlugin } from './components/scroll/plugins/SmoothScrollKeyboardPlugin';
|
|
41
42
|
import { SmoothScrollDragPlugin, NSmoothScrollDragPlugin } from './components/scroll/plugins/SmoothScrollDragPlugin';
|
|
43
|
+
import { ScrollSectionProgress, NScrollSectionProgress } from './components/scroll/section/ScrollSectionProgress';
|
|
42
44
|
|
|
43
45
|
import { SplitText, NSplitText } from './components/text/SplitText';
|
|
44
46
|
|
|
@@ -48,6 +50,7 @@ const utils = {
|
|
|
48
50
|
common,
|
|
49
51
|
listeners,
|
|
50
52
|
math,
|
|
53
|
+
scroll,
|
|
51
54
|
};
|
|
52
55
|
|
|
53
56
|
|
|
@@ -91,6 +94,7 @@ export {
|
|
|
91
94
|
ScrollView, NScrollView,
|
|
92
95
|
SmoothScrollKeyboardPlugin, NSmoothScrollKeyboardPlugin,
|
|
93
96
|
SmoothScrollDragPlugin, NSmoothScrollDragPlugin,
|
|
97
|
+
ScrollSectionProgress, NScrollSectionProgress,
|
|
94
98
|
|
|
95
99
|
SplitText, NSplitText,
|
|
96
100
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ScrollLike } from '../types/general';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get scroll values of a certain element
|
|
5
|
+
*/
|
|
6
|
+
export default function getScrollValues (
|
|
7
|
+
selector: (Window | Element | ScrollLike | undefined) = window,
|
|
8
|
+
) {
|
|
9
|
+
if (selector) {
|
|
10
|
+
const scrollTop = selector instanceof Window
|
|
11
|
+
? selector.pageYOffset : selector.scrollTop;
|
|
12
|
+
const scrollLeft = selector instanceof Window
|
|
13
|
+
? selector.pageXOffset : selector.scrollLeft;
|
|
14
|
+
return {
|
|
15
|
+
scrollTop,
|
|
16
|
+
scrollLeft,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Timeline } from '../../components/timeline/Timeline';
|
|
2
|
+
import { ScrollLike } from '../types/general';
|
|
3
|
+
import getScrollValues from './getScrollValues';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
/**
|
|
7
|
+
* @default window
|
|
8
|
+
*/
|
|
9
|
+
container?: Window | Element | ScrollLike;
|
|
10
|
+
/**
|
|
11
|
+
* top padding
|
|
12
|
+
* @default 0
|
|
13
|
+
*/
|
|
14
|
+
top?: number;
|
|
15
|
+
/**
|
|
16
|
+
* left padding
|
|
17
|
+
* @default 0
|
|
18
|
+
*/
|
|
19
|
+
left?: number;
|
|
20
|
+
/**
|
|
21
|
+
* scroll duration
|
|
22
|
+
* @default 250
|
|
23
|
+
*/
|
|
24
|
+
duration?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Scroll to coordinates
|
|
29
|
+
*/
|
|
30
|
+
export default function scrollTo ({
|
|
31
|
+
container = window,
|
|
32
|
+
top = 0,
|
|
33
|
+
left = 0,
|
|
34
|
+
duration = 250,
|
|
35
|
+
}: Props) {
|
|
36
|
+
return new Promise<void>((
|
|
37
|
+
resolve, reject,
|
|
38
|
+
) => {
|
|
39
|
+
// save start values
|
|
40
|
+
const startValues = getScrollValues(container);
|
|
41
|
+
if (startValues) {
|
|
42
|
+
// create animation
|
|
43
|
+
const timeline = new Timeline({
|
|
44
|
+
duration,
|
|
45
|
+
});
|
|
46
|
+
timeline.addCallback('progress', (data) => {
|
|
47
|
+
if (container) {
|
|
48
|
+
container.scrollTo({
|
|
49
|
+
top: startValues.scrollTop
|
|
50
|
+
+ (top - startValues.scrollTop) * data.easing,
|
|
51
|
+
left: startValues.scrollLeft
|
|
52
|
+
+ (left - startValues.scrollLeft) * data.easing,
|
|
53
|
+
behavior: 'auto',
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
timeline.addCallback('end', () => {
|
|
58
|
+
resolve();
|
|
59
|
+
});
|
|
60
|
+
timeline.play();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
reject();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { selectOne } from 'vevet-dom';
|
|
2
|
+
import { ScrollLike } from '../types/general';
|
|
3
|
+
import getScrollValues from './getScrollValues';
|
|
4
|
+
import clamp from '../math/clamp';
|
|
5
|
+
import scrollTo from './scrollTo';
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
/**
|
|
9
|
+
* @default window
|
|
10
|
+
*/
|
|
11
|
+
container?: Window | Element | ScrollLike;
|
|
12
|
+
/**
|
|
13
|
+
* The target element
|
|
14
|
+
*/
|
|
15
|
+
el: Element | string;
|
|
16
|
+
/**
|
|
17
|
+
* top padding
|
|
18
|
+
* @default 0
|
|
19
|
+
*/
|
|
20
|
+
top?: number;
|
|
21
|
+
/**
|
|
22
|
+
* left padding
|
|
23
|
+
* @default 0
|
|
24
|
+
*/
|
|
25
|
+
left?: number;
|
|
26
|
+
/**
|
|
27
|
+
* scroll duration
|
|
28
|
+
* @default 250
|
|
29
|
+
*/
|
|
30
|
+
duration?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Scroll to element
|
|
35
|
+
*/
|
|
36
|
+
export default function scrollToElement ({
|
|
37
|
+
container = window,
|
|
38
|
+
el,
|
|
39
|
+
top = 0,
|
|
40
|
+
left = 0,
|
|
41
|
+
duration = 250,
|
|
42
|
+
}: Props) {
|
|
43
|
+
return new Promise<void>((
|
|
44
|
+
resolve, reject,
|
|
45
|
+
) => {
|
|
46
|
+
const startValues = getScrollValues(container);
|
|
47
|
+
if (startValues) {
|
|
48
|
+
const element = selectOne(el);
|
|
49
|
+
if (element) {
|
|
50
|
+
const bounding = element.getBoundingClientRect();
|
|
51
|
+
const toTop = clamp(
|
|
52
|
+
bounding.top + startValues.scrollTop - top,
|
|
53
|
+
[0, Infinity],
|
|
54
|
+
);
|
|
55
|
+
const toLeft = clamp(
|
|
56
|
+
bounding.left + startValues.scrollLeft - left,
|
|
57
|
+
[0, Infinity],
|
|
58
|
+
);
|
|
59
|
+
scrollTo({
|
|
60
|
+
container,
|
|
61
|
+
top: toTop,
|
|
62
|
+
left: toLeft,
|
|
63
|
+
duration,
|
|
64
|
+
}).then(() => {
|
|
65
|
+
resolve();
|
|
66
|
+
}).catch(() => {
|
|
67
|
+
reject();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
reject();
|
|
73
|
+
});
|
|
74
|
+
}
|