-
Notifications
You must be signed in to change notification settings - Fork 1
/
history-restoration-timing.html
113 lines (112 loc) · 2.1 KB
/
history-restoration-timing.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title></title>
<style>
#page1 {
background: lightblue;
}
#page2 {
background: lightyellow;
}
#message {
background: #333;
color: white;
}
</style>
</head>
<body>
<div id="page1">
<p>Keep scroll down, man</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p>13</p>
<p>14</p>
<p>15</p>
<p>16</p>
<p>17</p>
<p>18</p>
<p>19</p>
<p>20</p>
<p><button id="pushstate">Click To PushState</button></p>
<p> history.scrollRestoration: <span id="SRValue"></span></p>
<p id="message"></p>
<p>21</p>
<p>22</p>
<p>23</p>
<p>24</p>
<p>25</p>
<p>26</p>
<p>27</p>
<p>28</p>
<p>29</p>
<p>30</p>
</div>
<div id="page2" style="display:none">
<p>Now click the Back button in your browser, let's see whether scroll is restored.</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p>13</p>
<p>14</p>
<p>15</p>
<p>16</p>
<p>17</p>
<p>18</p>
<p>19</p>
<p>20</p>
<p>21</p>
<p>22</p>
<p>23</p>
<p>24</p>
<p>25</p>
<p>26</p>
<p>27</p>
<p>28</p>
<p>29</p>
<p>30</p>
</div>
<script>
SRValue.innerText = history.scrollRestoration;
pushstate.addEventListener('click', function () {
history.pushState({}, 'title', '/page2')
page1.style.display = 'none'
page2.style.display = 'block'
window.scrollTo(0, 0)
})
window.addEventListener('popstate', function () {
var msg = 'scrollY onpopstate: ' + window.scrollY + '<br/>'
page1.style.display = 'block'
page2.style.display = 'none'
requestAnimationFrame(function() {
msg += '1 frame after popstate: ' + window.scrollY + '<br/>'
setTimeout(function () {
msg += '500ms after popstate: ' + window.scrollY
message.innerHTML = msg
}, 500)
})
})
</script>
</body>
</html>