-
Notifications
You must be signed in to change notification settings - Fork 6
/
block-overscroll.html
168 lines (140 loc) · 4.74 KB
/
block-overscroll.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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<style>
html {
overflow-x: hidden;
}
body {
height: 110vh;
margin: 0;
}
button {
width: 100%;
}
div {
margin-top: 10px;
}
#instructions {
margin-left: 8px;
}
#instructions.hidden {
display: none;
}
#log {
width: 90%;
height: 40%;
font-size: xx-small;
}
.blockOverscroll {
overscroll-behavior: none;
}
@media (pointer: coarse) {
[type="checkbox"]{
width: 2em;
height: 2em;
}
}
</style>
<script>
let boxTouchmoves;
let boxTouchmovesAll;
let boxOverscrollBehavior;
let instructions;
let log;
let numTouchmoves = 0;
function logEvent(event, canceled) {
let line = `${numTouchmoves} move prevented[${canceled}] cancelable[${event.cancelable}]`
let curLog = log.value;
log.value = line + '\n' + curLog;
}
function moveHandler(event) {
if (event.target == log)
return;
++numTouchmoves;
let canceled = false;
if ((boxTouchmoves.checked && numTouchmoves > 10) || boxTouchmovesAll.checked) {
event.preventDefault();
canceled = true;
}
logEvent(event, canceled);
}
function startHandler(event) {
if (event.target == log)
return;
numTouchmoves = 0;
log.value = '';
}
function reset() {
document.documentElement.classList.remove('blockOverscroll');
if (boxOverscrollBehavior.checked) {
document.documentElement.classList.add('blockOverscroll');
}
}
onload = () => {
boxTouchmoves = document.getElementById("checkboxTouchmoves");
boxTouchmovesAll = document.getElementById("checkboxTouchmovesAll");
boxOverscrollBehavior = document.getElementById("checkboxOverscrollBehavior");
instructions = document.getElementById('instructions')
log = document.getElementById("log");
boxTouchmoves.addEventListener('click', reset);
boxOverscrollBehavior.addEventListener('click', reset);
document.documentElement.addEventListener('touchstart',
startHandler,
{passive: false});
document.documentElement.addEventListener('touchmove',
moveHandler,
{passive: false});
if (window.localStorage.getItem('instructionsHidden'))
instructions.classList.add('hidden');
document.getElementById("toggle-instructions").addEventListener('click', () => {
instructions.classList.toggle('hidden');
localStorage.setItem('instructionsHidden', instructions.classList.contains('hidden'));
});
};
</script>
</head>
<body>
<div style="margin-top: 0">
<button id="toggle-instructions">Toggle Instructions</button>
</div>
<div id="instructions">
<p>
Use this page to test the effect of overscrolling on cancellability of touchmoves.
</p>
<p>
The first checkbox will call preventDefault on all touchmoves after the
10th until the touch gesture ends. Note that scrolling cannot be blocked
once started...unless you start to overscroll. (e.g. try scrolling until
the 10+ touch move, then hit the extent). Today, this allows blocking
overscroll glow and pull-to-refresh effects.
</p>
<p>
The second checkbox unconditionally preventDefaults all touchmoves. The
first touchmove is always blocking and will remain so, thus this will
continue to allow preventing overscroll effects if the first touchmove
would overscroll.
</p>
<p>
The third checkbox adds |overscroll-behavior:none| on the
documentElement, using CSS to prevent browser overscroll actions like
pull-to-refresh and the overscroll glow.
</p>
</div>
<div>
<input type="checkbox" id="checkboxTouchmoves">
<label for="checkboxTouchmoves">preventDefault after 10th touchmove</label>
</div>
<div>
<input type="checkbox" id="checkboxTouchmovesAll">
<label for="checkboxTouchmovesAll">preventDefault all touchmoves</label>
</div>
<div>
<input type="checkbox" id="checkboxOverscrollBehavior">
<label for="checkboxOverscrollBehavior">Set overscroll-behavior: none</label>
</div>
<div>Event Log:</div>
<textarea id="log" readonly></textarea>
</body>
</html>