forked from component/scroll-to
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (46 loc) · 889 Bytes
/
index.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
/**
* Module dependencies.
*/
var Tween = require('tween');
var raf = require('raf');
/**
* Expose `elementScrollTop`.
*/
module.exports = elementScrollTop;
/**
* Scroll `_element` to `top`.
*
* @param {Element} _element
* @param {Number} top
* @param {Object} options
* @api public
*/
function elementScrollTop(_element, top, options) {
options = options || {};
// start position
const start = {
top: _element.scrollTop
};
// setup tween
const tween = Tween(start)
.ease(options.ease || 'out-circ')
.to({
top: top
})
.duration(options.duration || 1000);
// scroll
tween.update(function (o) {
_element.scrollTop = o.top;
});
// handle end
tween.on('end', function () {
animate = function () {};
});
// animate
function animate() {
raf(animate);
tween.update();
}
animate();
return tween;
}