forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroller.js
79 lines (70 loc) · 2.63 KB
/
scroller.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
76
77
78
79
const getDefaultConfig = (randomizer, window) => {
const document = window.document;
const documentElement = document.documentElement;
const body = document.body;
const defaultPositionSelector = () => {
const documentWidth = Math.max(
body.scrollWidth,
body.offsetWidth,
documentElement.scrollWidth,
documentElement.offsetWidth,
documentElement.clientWidth
);
const documentHeight = Math.max(
body.scrollHeight,
body.offsetHeight,
documentElement.scrollHeight,
documentElement.offsetHeight,
documentElement.clientHeight
);
return [
randomizer.natural({
max: documentWidth - documentElement.clientWidth,
}),
randomizer.natural({
max: documentHeight - documentElement.clientHeight,
}),
];
};
const defaultShowAction = (scrollX, scrollY) => {
const scrollSignal = document.createElement('div');
scrollSignal.style.zIndex = 2000;
scrollSignal.style.border = '3px solid red';
scrollSignal.style.width = documentElement.clientWidth - 25 + 'px';
scrollSignal.style.height = documentElement.clientHeight - 25 + 'px';
scrollSignal.style.position = 'absolute';
scrollSignal.style.webkitTransition = 'opacity 1s ease-out';
scrollSignal.style.mozTransition = 'opacity 1s ease-out';
scrollSignal.style.transition = 'opacity 1s ease-out';
scrollSignal.style.left = scrollX + 10 + 'px';
scrollSignal.style.top = scrollY + 10 + 'px';
scrollSignal.style['pointer-events'] = 'none';
const element = body.appendChild(scrollSignal);
setTimeout(() => {
body.removeChild(element);
}, 1000);
setTimeout(() => {
element.style.opacity = 0;
}, 50);
};
return {
positionSelector: defaultPositionSelector,
showAction: defaultShowAction,
log: false,
};
};
export default (userConfig) => ({ logger, randomizer, window }) => {
const config = { ...getDefaultConfig(randomizer, window), ...userConfig };
return () => {
const position = config.positionSelector();
const scrollX = position[0];
const scrollY = position[1];
window.scrollTo(scrollX, scrollY);
if (typeof config.showAction === 'function') {
config.showAction(scrollX, scrollY);
}
if (logger && config.log) {
logger.log('gremlin', 'scroller ', 'scroll to', scrollX, scrollY);
}
};
};