-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
scrollTo.js
75 lines (67 loc) · 1.65 KB
/
scrollTo.js
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
/* Scroll to a target with animation.
*
* |Name |Desc |
* |-------|--------------|
* |target |Scroll target |
* |options|Scroll options|
*
* ### options
*
* |Name |Desc |
* |---------------|---------------------------------------|
* |tolerance=0 |Tolerance of target to scroll |
* |duration=800 |Scroll duration |
* |easing=outQuart|Easing function |
* |callback=noop |Function to run once scrolling complete|
*/
/* example
* scrollTo('body', {
* tolerance: 0,
* duration: 800,
* easing: 'outQuart',
* callback: function() {}
* });
*/
/* module
* env: browser
*/
/* typescript
* export declare function scrollTo(
* target: Element | string | number,
* options: {
* tolerance?: number;
* duration?: number;
* easing?: string | Function;
* callback?: types.AnyFn;
* }
* );
*/
_('Tween defaults noop isNum $offset types');
exports = function(target, options) {
options = options || {};
defaults(options, defOpts);
if (!isNum(target)) target = $offset(target).top;
new Tween({
y: window.pageYOffset
})
.on('update', function(target) {
window.scroll(0, target.y);
})
.on('end', function() {
options.callback();
})
.to(
{
y: target - options.tolerance
},
options.duration,
options.easing
)
.play();
};
const defOpts = {
tolerance: 0,
duration: 800,
easing: 'outQuart',
callback: noop
};