-
Notifications
You must be signed in to change notification settings - Fork 41
/
scroll-blocks-on.html
257 lines (235 loc) · 7.09 KB
/
scroll-blocks-on.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width">
<title>scroll-blocks-on test page</title>
<style>
html, body {
margin: 0;
}
#config {
margin: 10px;
}
#div {
overflow: scroll;
height: 400px;
border: 2px solid blue;
}
#warning {
color: red;
}
#content {
position: absolute;
width: 100%;
height: 2000px;
background: linear-gradient(lightgreen, lightgreen 50%, white 50%, white);
background-size: 100% 200px;
}
#syncscroll {
position: fixed;
border: 3px solid red;
box-sizing: border-box;
right: 0;
height: 100px;
width: 50%;
background-color: pink;
}
#log {
position: fixed;
bottom: 0;
width: 100%;
height: 30%;
overflow: scroll;
border: 2px solid black;
background-color: lightgrey;
box-sizing: border-box;
padding: 8px;
}
#nested {
position: absolute;
top: 700px;
left: 50%;
right: 0;
border: 2px solid black;
box-sizing: border-box;
background-color: lightblue;
height: 1000px;
}
</style>
<script>
function getDelay()
{
return parseInt(document.getElementById('delay').value);
}
var counter = 0;
function doSomethingDumb()
{
// This is a particularly dumb example of adding jank to a web page, but in reality
// it can be much more subtle. Other complex javascript, expensive layout and loading/decoding images
// might take more than 16ms, resulting in at least one dropped frame when scrolling.
counter++;
console.log('Doing something dumb #' + counter);
var start = Date.now();
var end = 0;
var delay = getDelay();
while (end < start + delay)
end = Date.now();
console.log('Done');
}
var pendingLog = {
type: undefined,
count: 0,
cancelable: false,
scrollCount: 0
}
function flushPendingLogEntry()
{
if (pendingLog.count || pendingLog.scrollCount) {
var newLog = document.createElement('DIV');
newLog.className = 'logentry';
var logStr = '';
if (pendingLog.count) {
logStr += pendingLog.type;
if (pendingLog.count > 1) {
logStr += '[' + pendingLog.count + ']';
}
logStr += ' cancelable=' + pendingLog.cancelable;
if (pendingLog.scrollCount) {
logStr += ', ';
}
}
if (pendingLog.scrollCount) {
logStr += 'scroll';
if (pendingLog.scrollCount > 1) {
logStr += '[' + pendingLog.scrollCount + ']';
}
}
newLog.innerHTML = logStr;
document.getElementById('log').appendChild(newLog);
document.getElementById('log').scrollTop = document.getElementById('log').scrollHeight;
pendingLog = {
type: undefined,
count: 0,
cancelable: false,
scrollCount: 0
}
}
if (logTimer)
window.clearTimeout(logTimer);
logTimer = undefined;
}
var logTimer = undefined;
function scheduleLogUpdate()
{
if (logTimer)
window.clearTimeout(logTimer);
logTimer = window.setTimeout(flushPendingLogEntry, 100);
}
function inputHandler(e)
{
if (pendingLog.count &&
(pendingLog.type != e.type || pendingLog.cancelable != e.cancelable)) {
flushPendingLogEntry();
}
pendingLog.type = e.type;
pendingLog.cancelable = e.cancelable;
pendingLog.count++;
scheduleLogUpdate();
}
function scrollHandler(e)
{
document.getElementById('syncscroll').style.transform =
'translate3d(0, ' + (-1 * window.scrollY) + 'px, 0)';
pendingLog.scrollCount++;
scheduleLogUpdate();
}
function onResize()
{
document.getElementById('syncscroll').style.top =
document.getElementById('content').getBoundingClientRect().top;
}
function computeScrollBlocksOn(idPrefix)
{
var sbo = '';
if (document.getElementById(idPrefix + '-touch').checked)
sbo += 'start-touch ';
if (document.getElementById(idPrefix + '-wheel').checked)
sbo += 'wheel-event ';
if (document.getElementById(idPrefix + '-scroll').checked)
sbo += 'scroll-event ';
if (!sbo)
return 'none';
return sbo;
}
var timer;
function checkboxUpdated()
{
if (document.getElementById('dowork').checked) {
if (!timer)
timer = window.setInterval(doSomethingDumb, getDelay() + 200);
} else {
if (timer) {
window.clearInterval(timer);
timer = undefined;
}
}
document.documentElement.style.scrollBlocksOn =
computeScrollBlocksOn('main');
document.getElementById('nested').style.scrollBlocksOn =
computeScrollBlocksOn('nested');
document.getElementById('log').style.display =
document.getElementById('showlog').checked ? 'block' : 'none';
}
window.onload = function() {
if ('scrollBlocksOn' in document.documentElement.style) {
document.getElementById('warning').style.display = 'none';
document.getElementById('initial').innerHTML = getComputedStyle(document.documentElement).scrollBlocksOn;
}
var cbs = document.querySelectorAll('input[type=checkbox]');
for (var i=0; i < cbs.length; i++) {
cbs[i].addEventListener('change', checkboxUpdated);
}
document.addEventListener('touchstart', inputHandler);
document.addEventListener('touchmove', inputHandler);
document.addEventListener('touchend', inputHandler);
document.addEventListener('touchcancel', inputHandler);
document.addEventListener('wheel', inputHandler);
document.addEventListener('scroll', scrollHandler);
document.addEventListener('resize', onResize);
checkboxUpdated();
onResize();
}
</script>
</head>
<body>
<div id=config>
<p>This page demonstrates the effect of the <a href="http://crbug.com/347272">proposed scroll-blocks-on CSS property</a>.
<p>When enabled, this page will do a bunch of work in JavaScript to simulate a complex/janky application. Try scrolling on a touchscreen or with a touchpad using various settings for scroll-blocks-on.
<hr>
<input type='checkbox' id='dowork'>Do lots of work on the main thread<br>
Delay in ms: <input id='delay' value='32'><br>
<input type='checkbox' id='showlog'>Show event log<br>
<b id=warning>WARNING: scroll-blocks-on is not supported by your browser. Use Chrome 41+ with the enable-experimental-web-platform-features flag set.<br></b>
Initial document scroll-blocks-on: <span id=initial></span><br>
<br>
<b>Scroll-blocks-on:</b><br>
<input type='checkbox' id='main-touch' checked>start-touch<br>
<input type='checkbox' id='main-wheel' checked>wheel-event<br>
<input type='checkbox' id='main-scroll'>scroll-event<br>
<hr>
</div>
<div id=content>
<div id=nested>
This DIV has it's own scroll-blocks-on setting which can augment that set by the document.<br><br>
<b>Scroll-blocks-on:</b><br>
<input type='checkbox' id='nested-touch'>start-touch<br>
<input type='checkbox' id='nested-wheel'>wheel-event<br>
<input type='checkbox' id='nested-scroll'>scroll-event<br>
</div>
</div>
<div id=syncscroll>
This div is positioned in JavaScript to match the position of the first green bar.
</div>
<div id=log>
</div>
</body>
</html>