-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (63 loc) · 2.32 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var Transient = require('transient');
module.exports = function (options) {
// do nothing if not running in the browser
if (typeof window === 'undefined' || !window.scroll) {
return;
}
options = options || {};
options.element = options.element || window;
options.duration = options.duration || 150; // 150 ms
options.x = options.x || 0; // default to scrolling to lefthand side of the page
options.y = options.y || 0; // default to scrolling to very top of page
// Set scroll or window and non window elements
var scrollTo;
var xStartPosition;
var yStartPosition;
if (options.element === window) {
scrollTo = function (x, y) {
window.scroll(x, y);
};
xStartPosition = window.scrollX;
yStartPosition = window.scrollY;
} else {
scrollTo = function (x, y) {
options.element.scrollTop = y;
options.element.scrollLeft = x;
};
xStartPosition = options.element.scrollLeft;
yStartPosition = options.element.scrollTop;
}
var a = new Transient({
duration: options.duration,
draw: function (progress) {
/*
scrolling down from 0px (top of page) to 1000px?
When progress is 0, newScrollPosition is unchanged: 0px;
When progress is .50, newScrollPosition is 1500px;
When progress is .75, newScrollPosition is 1750px;
scrolling up from 1000px to 0px (top of page)?
When progress is 0, newScrollPosition is unchanged: 1000px
When progress is .50 newScrollPosition is 500px
When progress is .75 newScrollPosition is 250px
totalScrollChange can be negative when scrolling up:
newScrollPosition = (totalScrollChange * progress) + yStartPosition
totalScrollChange = targetPosition - yStartPosition;
yStartPosition + totalScrollChange * progress
newScrollPosition = yStartPosition + ((targetPosition - yStartPosition) * progress)
*/
scrollTo(xStartPosition + (options.x - xStartPosition) * progress, yStartPosition + (options.y - yStartPosition) * progress);
},
onCancel: function () {
// Do nothing, this just overrides the behavior of transient which is to call onEnd
},
// Since Transient updates progress before calling draw(), then calls onEnd if progress >= 1,
// it may not call draw() with 100% progress. Here we finish the scrolling onEnd.
onEnd: function () {
scrollTo(options.x, options.y);
}
});
a.start();
return function () {
a.cancel();
};
};