This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (92 loc) · 2.89 KB
/
Copy pathindex.js
File metadata and controls
104 lines (92 loc) · 2.89 KB
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
'use strict';
var debounce = require('debounce');
var xtend = require('xtend');
var getWindow = require('./util/get_window').getWindow;
var removeListenerFunctions;
function end() {
for (var i = 0, l = removeListenerFunctions.length; i < l; i++) {
removeListenerFunctions[i]();
}
}
function getSavedScroll(input) {
input = input || getWindow().history;
if (!input || !input.state) return;
return input.state.scroll;
}
function restoreScroll(input, attemptsRemaining) {
attemptsRemaining = attemptsRemaining == null ? 5 : attemptsRemaining;
var savedScroll = getSavedScroll(input);
if (!savedScroll) return;
getWindow().requestAnimationFrame(function() {
var savedX = savedScroll.x;
var savedY = savedScroll.y;
if (attemptsRemaining === 0) return;
var pageXOffset = getWindow().pageXOffset;
var pageYOffset = getWindow().pageYOffset;
if (
savedY < getWindow().document.body.scrollHeight &&
(savedX !== pageXOffset || savedY !== pageYOffset)
) {
getWindow().scrollTo(savedX, savedY);
} else {
restoreScroll(input, attemptsRemaining - 1);
}
});
}
function start(options) {
options = xtend(
{
autoRestore: true,
captureScrollDebounce: 50
},
options || {}
);
var captureScroll = debounce(function() {
getWindow().requestAnimationFrame(function() {
var x = getWindow().pageXOffset;
var y = getWindow().pageYOffset;
var historyState = getWindow().history.state;
var savedX = historyState && historyState.scroll && historyState.scroll.x;
var savedY = historyState && historyState.scroll && historyState.scroll.y;
if (savedX !== x || savedY !== y) {
var nextHistoryState = xtend(historyState, {
scroll: { x: x, y: y }
});
getWindow().history.replaceState(
nextHistoryState,
null,
getWindow().location
);
}
});
}, options.captureScrollDebounce);
// cf. https://developers.google.com/web/updates/2015/09/history-api-scroll-restoration
if ('scrollRestoration' in getWindow().history) {
getWindow().history.scrollRestoration = 'manual';
}
// Scroll positions are saved into the history entry's state; then when that
// the history changes (the popstate event), we try restoring any saved
// scroll position.
getWindow().addEventListener('scroll', captureScroll, { passive: true });
if (options.autoRestore) {
getWindow().addEventListener('popstate', restoreScroll);
}
removeListenerFunctions = [
function() {
getWindow().removeEventListener('scroll', captureScroll, {
passive: true
});
}
];
if (options.autoRestore) {
removeListenerFunctions.push(function() {
getWindow().removeEventListener('popstate', restoreScroll);
});
}
}
module.exports = {
start: start,
end: end,
restoreScroll: restoreScroll,
getSavedScroll: getSavedScroll
};