-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtouch-timeout.html
182 lines (170 loc) · 4.28 KB
/
touch-timeout.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
<!DOCTYPE html>
<html>
<head>
<meta name=viewport content=''/>
<title>Touch event timeout test</title>
<style>
html,body {
height: 100%;
box-sizing: border-box;
margin: 0;
}
body {
padding: 5px;
}
#targets {
border: 1px solid blue;
height: 120px;
position: relative;
}
#targets > div {
position: absolute;
top: 10px;
width: 100px;
height: 100px;
}
#tgt-normal {
left: 10px;
background-color: lightblue;
}
#tgt-scroll {
overflow: scroll;
background-color: lightgreen;
left: 120px;
}
.spacer {
height: 400px;
}
#tgt-tanone {
touch-action: none;
background-color: pink;
left: 230px;
}
#log {
border: 1px solid black;
margin: 5px;
overflow: auto;
min-height: 50px;
flex: 1 1;
}
@media screen and (min-width: 800px) {
#targets {
height: 220px;
}
#targets > div {
width: 200px;
height: 200px;
}
#tgt-scroll {
left: 220px;
}
#tgt-tanone {
left: 430px;
}
html {
font-size: 30px;
}
#config input[type=checkbox] {
height: 30px;
width: 30px;
}
}
</style>
</head>
<body>
<div id=targets>
<div id=tgt-normal>
Normal div
</div>
<div id=tgt-scroll>
scrollable content
<div class=spacer></div>
end
</div>
<div id=tgt-tanone>
touch-action: none
</div>
</div>
<div id=log>
Log<br>
</div>
<div id=config>
<div><input type=checkbox id=show-log checked>Show log</div>
<div><input type=checkbox id=mobile-viewport>Use mobile viewport</div>
<div><input type=checkbox id=consume-touchstart>Consume touchstart</div>
<div><input type=checkbox id=consume-touchmove checked>Consume touchmove</div>
<div><input type=checkbox id=delay-touchstart>Delay touchstart</div>
<div><input type=checkbox id=delay-first-move checked>Delay first touchmove</div>
<div><input type=checkbox id=delay-second-move>Delay second touchmove</div>
<div><input type=number id=delay value=300>Delay in ms</div>
</div>
<script>
var log = document.getElementById('log');
var logString = '';
var lastTime = 0;
function writeLog(str) {
var newTime = Date.now(); // performance.now not supported on mobile safari?
logString += str + (lastTime ? (' ' + Math.round(newTime - lastTime) + 'ms') : '') + '<br>\n';
lastTime = newTime;
// Don't allow layout to contribute to event handling time
window.setTimeout(function() {
log.innerHTML = logString;
}, 0);
}
var moveCount = 0;
function touchHandler(e) {
var doDelay = false;
var logStr = e.type;
var consume = false;
if (e.type == 'touchstart') {
logString = '';
moveCount = 0;
doDelay = document.getElementById('delay-touchstart').checked;
if (document.getElementById('consume-touchstart').checked)
consume = true;
} else if (e.type == 'touchmove') {
moveCount++;
logStr += ' [' + moveCount + ']';
if (moveCount == 1)
doDelay = document.getElementById('delay-first-move').checked;
if (moveCount == 2)
doDelay = document.getElementById('delay-second-move').checked;
if (document.getElementById('consume-touchmove').checked)
consume = true;
}
if (consume) {
e.preventDefault();
logStr += ' (consumed)';
}
if (doDelay) {
var delay = parseInt(document.getElementById('delay').value);
logStr += ' (delayed ' + delay + 'ms)';
var start = Date.now();
var end = 0;
while (end < start + delay)
end = Date.now();
}
writeLog(logStr);
}
var targets = document.getElementById('targets');
targets.addEventListener('touchstart', touchHandler);
targets.addEventListener('touchmove', touchHandler);
targets.addEventListener('touchend', touchHandler);
targets.addEventListener('touchcancel', touchHandler);
var taNone = document.getElementById('tgt-tanone');
if (!getComputedStyle(taNone).touchAction)
taNone.style.display = 'none';
document.getElementById('mobile-viewport').addEventListener('change', function(e) {
var viewport = document.querySelector("meta[name=viewport]");
var value = '';
if (e.target.checked) {
value = 'width=device-width';
}
viewport.setAttribute('content', value);
});
document.getElementById('show-log').addEventListener('change', function(e) {
log.style.display = e.target.checked ? "" : "none";
});
</script>
</body>
</html>