This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
demo.html
80 lines (75 loc) · 2.38 KB
/
demo.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Prevent Popstate Scroll demo</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<script>var canControlScrollRestoration = 'scrollRestoration' in window.history;</script>
<script src="dist/scroll-restoration-polyfill.browser.js"></script>
<script>var canControlScrollRestorationNow = 'scrollRestoration' in window.history;</script>
<style>
html {
font-family: sans-serif;
font-size: 1.3em;
height: 5000px;
background: linear-gradient(#fff, #408000);
}
.overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
padding: 1em;
background: rgba(255,255,255,.8);
}
label {
font-style: italic;
}
::-webkit-scrollbar {
width: 1em;
}
::-webkit-scrollbar-thumb {
background-color: #408000;
outline: 1px solid white;
}
</style>
</head>
<body>
<div class="overlay">
<p>Support? <strong><script>document.write(canControlScrollRestoration?"Native":canControlScrollRestorationNow?"Via polyfill":"Polyfill failed. Open an issue on GitHub please!")</script></strong></p>
<ol>
<li>Try simulating 5 scrolls+navigations: <button type="button" id="add-demo-entries">Simulate</button></li>
<li>Go back 5 times, notice jumping behavior.</li>
</ol>
<ol>
<li>Now simulate again</li>
<li>Tick this box: <input type="checkbox" id="prevention">
<label for="prevention"><code>history.scrollRestoration = "manual"</code></label></li>
<li>Go back 5 times, notice that the scrolling remains constant</li>
</ol>
<p><code>history.scrollRestoration = "auto"</code> is the default behavior: scroll is restored to where the user was before changing URL (via <code>pushState</code>, or <code>location.hash</code>)</p>
</div>
<script>
var checkbox = document.getElementById('prevention');
var simulate = document.getElementById('add-demo-entries');
function updatePreference () {
history.scrollRestoration = checkbox.checked?'manual':'auto';
}
var count = 1;
function addDemoEntries () {
var start = count;
var timer = setInterval(function () {
window.scrollTo(0, Math.random()*4000);
location.hash = '#'+count;
count++;
if (count > start + 4) {
clearInterval(timer);
}
}, 100);
}
checkbox.addEventListener('change', updatePreference);
simulate.addEventListener('click', addDemoEntries);
updatePreference();
</script>
</body>
</html>