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
167
168
169
170
171
172
|
const SCROLL_DELTA_X = 48;
const SCROLL_DELTA_Y = 48;
const SMOOTH_SCROLL_DURATION = 150;
const isVisible = (element) => {
let rect = element.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) {
return false;
}
if (rect.right < 0 && rect.bottom < 0) {
return false;
}
if (window.innerWidth < rect.left && window.innerHeight < rect.top) {
return false;
}
let { display, visibility } = window.getComputedStyle(element);
if (display === 'none' || visibility === 'hidden') {
return false;
}
return true;
};
const isScrollableStyle = (element) => {
let { overflowX, overflowY } = window.getComputedStyle(element);
return !(overflowX !== 'scroll' && overflowX !== 'auto' &&
overflowY !== 'scroll' && overflowY !== 'auto');
};
const isOverflowed = (element) => {
return element.scrollWidth > element.clientWidth ||
element.scrollHeight > element.clientHeight;
};
// Find a visiable and scrollable element by depth-first search. Currently
// this method is called by each scrolling, and the returned value of this
// method is not cached. That does not cause performance issue because in the
// most pages, the window is root element i,e, documentElement.
const findScrollable = (element) => {
if (isScrollableStyle(element) && isOverflowed(element)) {
return element;
}
let children = Array.prototype
.filter.call(element.children, e => isVisible(e));
for (let child of children) {
let scrollable = findScrollable(child);
if (scrollable) {
return scrollable;
}
}
return null;
};
const scrollTarget = () => {
if (isOverflowed(window.document.documentElement)) {
return window.document.documentElement;
}
if (isOverflowed(window.document.body)) {
return window.document.body;
}
let target = findScrollable(window.document.documentElement);
if (target) {
return target;
}
return window.document.documentElement;
};
const easing = (t) => {
if (t < 1) {
return t * t;
}
return -(t - 1) * (t - 1) + 1;
};
const smoothScroll = (element, x, y) => {
let startX = element.scrollTop;
let startY = element.scrollTop;
let distanceX = x - startX;
let distanceY = y - startY;
let timeStart = 0;
const loop = (timeCurrent) => {
if (!timeStart) {
timeStart = timeCurrent;
}
let timeElapsed = timeCurrent - timeStart;
let t = timeElapsed / SMOOTH_SCROLL_DURATION;
let nextX = startX + distanceX * easing(t);
let nextY = startY + distanceY * easing(t);
window.scrollTo(nextX, nextY);
if (timeElapsed < SMOOTH_SCROLL_DURATION) {
window.requestAnimationFrame(loop);
} else {
element.scrollTo(x, y);
}
};
window.requestAnimationFrame(loop);
};
const roughScroll = (element, x, y) => {
element.scrollTo(x, y);
};
const scroll = (element, x, y, smooth) => {
if (smooth) {
smoothScroll(element, x, y);
} else {
roughScroll(element, x, y);
}
};
const scrollVertically = (count, smooth) => {
let target = scrollTarget();
let x = target.scrollLeft;
let y = target.scrollTop + SCROLL_DELTA_Y * count;
scroll(target, x, y, smooth);
};
const scrollHorizonally = (count, smooth) => {
let target = scrollTarget();
let x = target.scrollLeft + SCROLL_DELTA_X * count;
let y = target.scrollTop;
scroll(target, x, y, smooth);
};
const scrollPages = (count, smooth) => {
let target = scrollTarget();
let height = target.clientHeight;
let x = target.scrollLeft;
let y = target.scrollTop + height * count;
scroll(target, x, y, smooth);
};
const scrollTop = (smooth) => {
let target = scrollTarget();
let x = target.scrollLeft;
let y = 0;
scroll(target, x, y, smooth);
};
const scrollBottom = (smooth) => {
let target = scrollTarget();
let x = target.scrollLeft;
let y = target.scrollHeight;
scroll(target, x, y, smooth);
};
const scrollHome = (smooth) => {
let target = scrollTarget();
let x = 0;
let y = target.scrollTop;
scroll(target, x, y, smooth);
};
const scrollEnd = (smooth) => {
let target = scrollTarget();
let x = target.scrollWidth;
let y = target.scrollTop;
scroll(target, x, y, smooth);
};
export {
scrollVertically, scrollHorizonally, scrollPages,
scrollTop, scrollBottom, scrollHome, scrollEnd
};
|