-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_version1.html
More file actions
567 lines (488 loc) · 15.9 KB
/
timer_version1.html
File metadata and controls
567 lines (488 loc) · 15.9 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LED手机计时器 - 版本1</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-tap-highlight-color: transparent;
touch-action: manipulation;
}
body, html {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
background-color: black;
color: #ff3333;
font-family: 'Courier New', Courier, monospace;
font-weight: bold;
overflow: hidden;
}
.timer {
width: 95%; /* 减少宽度留出边距 */
height: 45%;
display: flex;
justify-content: center;
align-items: center;
border: 4px solid #ff3333;
border-radius: 15px;
background-color: black;
position: relative;
overflow: hidden;
margin: 0 auto; /* 居中 */
}
.timer-text {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
text-shadow: 0 0 2px currentColor; /* 减少阴影模糊度 */
letter-spacing: 0.1vw; /* 进一步减少字母间距 */
padding: 0 3%;
overflow: hidden;
line-height: 1;
white-space: nowrap;
-webkit-font-smoothing: antialiased; /* 改善字体渲染 */
font-smooth: always;
}
.controls {
width: 95%;
height: 10%;
display: flex;
justify-content: space-around;
align-items: center;
background-color: black;
color: #ff3333;
font-size: 4vmin;
margin: 0 auto; /* 居中 */
}
.control-button {
padding: 2vmin;
border: 2px solid #ff3333;
border-radius: 10px;
background-color: black;
color: #ff3333;
cursor: pointer;
user-select: none;
}
.control-button:active {
background-color: #ff3333;
color: black;
}
.time-picker {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #222;
border: 2px solid #ff3333;
border-radius: 15px;
padding: 5vmin;
z-index: 100;
width: 90%;
max-width: 400px;
}
.time-picker.active {
display: block;
}
.time-selectors {
display: flex;
justify-content: space-around;
align-items: center;
margin-bottom: 4vmin;
}
.time-selector {
display: flex;
flex-direction: column;
align-items: center;
width: 30%;
}
.time-label {
font-size: 4vmin;
margin-bottom: 2vmin;
color: #fff;
}
.time-options {
height: 30vmin;
overflow-y: scroll;
width: 100%;
border: 1px solid #ff3333;
border-radius: 5px;
background-color: #000;
}
.time-option {
padding: 2vmin;
text-align: center;
font-size: 4vmin;
cursor: pointer;
color: #fff;
}
.time-option.selected {
background-color: #ff3333;
color: black;
}
.time-picker-buttons {
display: flex;
justify-content: space-around;
}
.time-picker-button {
padding: 2vmin 4vmin;
border: 2px solid #ff3333;
border-radius: 10px;
background-color: black;
color: #ff3333;
cursor: pointer;
font-size: 4vmin;
}
.time-picker-button:active {
background-color: #ff3333;
color: black;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
}
.blinking {
animation: blink 1s infinite;
}
/* 返回按钮 */
.back-button {
position: fixed;
top: 20px;
left: 20px;
padding: 10px 15px;
border: 2px solid #ff3333;
border-radius: 10px;
background-color: black;
color: #ff3333;
text-decoration: none;
font-size: 16px;
z-index: 50;
}
.back-button:active {
background-color: #ff3333;
color: black;
}
/* 针对小屏幕的额外优化 */
@media (max-width: 480px) and (max-height: 700px) {
.timer-text {
letter-spacing: 0.05vw;
}
}
/* 针对横屏的优化 */
@media (orientation: landscape) and (max-height: 500px) {
.controls {
height: 15%;
}
}
</style>
</head>
<body>
<a href="index.html" class="back-button">返回选择</a>
<div class="timer" id="timer1" style="transform: rotate(180deg);">
<div class="timer-text" id="timer1Text">00:00.00</div>
</div>
<div class="controls">
<div class="control-button" id="resetButton">重置</div>
<div class="control-button" id="modeButton">倒计时</div>
<div class="control-button" id="setTimeButton">设置时间</div>
</div>
<div class="timer" id="timer2">
<div class="timer-text" id="timer2Text">00:00.00</div>
</div>
<div class="time-picker" id="timePicker">
<div class="time-selectors">
<div class="time-selector">
<div class="time-label">分</div>
<div class="time-options" id="minuteOptions"></div>
</div>
<div class="time-selector">
<div class="time-label">秒</div>
<div class="time-options" id="secondOptions"></div>
</div>
</div>
<div class="time-picker-buttons">
<div class="time-picker-button" id="cancelButton">取消</div>
<div class="time-picker-button" id="confirmButton">确定</div>
</div>
</div>
<script>
// 状态变量
let isRunning = false;
let isCountdownMode = false;
let startTime;
let elapsedTime = 0;
let countdownTime = 0;
let timerInterval;
// DOM元素
const timer1Element = document.getElementById('timer1Text');
const timer2Element = document.getElementById('timer2Text');
const resetButton = document.getElementById('resetButton');
const modeButton = document.getElementById('modeButton');
const setTimeButton = document.getElementById('setTimeButton');
const timePicker = document.getElementById('timePicker');
const minuteOptions = document.getElementById('minuteOptions');
const secondOptions = document.getElementById('secondOptions');
const cancelButton = document.getElementById('cancelButton');
const confirmButton = document.getElementById('confirmButton');
// 初始化时间选择器
function initTimePicker() {
// 生成分钟选项 (0-59)
for (let i = 0; i < 60; i++) {
const option = document.createElement('div');
option.className = 'time-option';
option.textContent = i.toString().padStart(2, '0');
option.dataset.value = i;
minuteOptions.appendChild(option);
}
// 生成秒钟选项 (0-59)
for (let i = 0; i < 60; i++) {
const option = document.createElement('div');
option.className = 'time-option';
option.textContent = i.toString().padStart(2, '0');
option.dataset.value = i;
secondOptions.appendChild(option);
}
// 默认选择00:00
selectTimeOption(minuteOptions, 0);
selectTimeOption(secondOptions, 0);
}
// 选择时间选项
function selectTimeOption(container, value) {
const options = container.querySelectorAll('.time-option');
options.forEach(option => {
option.classList.remove('selected');
if (parseInt(option.dataset.value) === value) {
option.classList.add('selected');
option.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
});
}
// 获取选中的时间值
function getSelectedTime() {
const selectedMinute = parseInt(minuteOptions.querySelector('.selected').dataset.value);
const selectedSecond = parseInt(secondOptions.querySelector('.selected').dataset.value);
return {
minutes: selectedMinute,
seconds: selectedSecond,
totalMs: (selectedMinute * 60 + selectedSecond) * 1000
};
}
// 初始化
function init() {
initTimePicker();
// 重置按钮事件
resetButton.addEventListener('click', resetTimers);
// 模式切换按钮事件
modeButton.addEventListener('click', () => {
isCountdownMode = !isCountdownMode;
modeButton.textContent = isCountdownMode ? '正向计时' : '倒计时';
resetTimers();
// 只在倒计时模式下显示设置时间按钮
setTimeButton.style.display = isCountdownMode ? 'block' : 'none';
});
// 设置时间按钮事件
setTimeButton.addEventListener('click', () => {
timePicker.classList.add('active');
});
// 取消按钮事件
cancelButton.addEventListener('click', () => {
timePicker.classList.remove('active');
});
// 确定按钮事件
confirmButton.addEventListener('click', () => {
const selectedTime = getSelectedTime();
countdownTime = selectedTime.totalMs;
updateTimerDisplay(countdownTime);
timePicker.classList.remove('active');
});
// 时间选项点击事件
document.querySelectorAll('.time-options').forEach(container => {
container.addEventListener('click', (e) => {
if (e.target.classList.contains('time-option')) {
const value = parseInt(e.target.dataset.value);
selectTimeOption(container, value);
}
});
});
// 屏幕点击事件 - 开始/停止计时
document.addEventListener('click', (e) => {
// 如果点击的是控制按钮或时间选择器,不处理
if (e.target.closest('.controls') || e.target.closest('.time-picker') || e.target.closest('.back-button')) {
return;
}
toggleTimer();
});
// 防止手机息屏
document.addEventListener('touchstart', () => {
if (window.screen && window.screen.keepAwake !== undefined) {
window.screen.keepAwake = true;
}
});
// 窗口大小变化时调整字体
window.addEventListener('resize', adjustFontSize);
// 初始调整字体大小
adjustFontSize();
// 延迟再次调整,确保DOM完全加载
setTimeout(adjustFontSize, 100);
}
// 调整字体大小确保所有数字可见
function adjustFontSize() {
const timerTexts = document.querySelectorAll('.timer-text');
timerTexts.forEach(timerText => {
// 重置字体大小
timerText.style.fontSize = '';
// 获取容器尺寸
const container = timerText.parentElement;
const containerWidth = container.clientWidth;
const containerHeight = container.clientHeight;
// 使用更保守的初始字体大小
let fontSize = 80; // 降低初始字体大小
timerText.style.fontSize = fontSize + 'vmin';
// 获取文本尺寸
const textWidth = timerText.scrollWidth;
const textHeight = timerText.scrollHeight;
// 计算合适的字体大小,留出更多边距
const widthRatio = (containerWidth * 0.85) / textWidth; // 留出15%边距
const heightRatio = (containerHeight * 0.85) / textHeight; // 留出15%边距
const scale = Math.min(widthRatio, heightRatio, 1); // 确保不超过1
// 应用新字体大小
const finalFontSize = fontSize * scale;
timerText.style.fontSize = finalFontSize + 'vmin';
// 验证文本是否完全可见
setTimeout(() => {
const newTextWidth = timerText.scrollWidth;
const newTextHeight = timerText.scrollHeight;
// 如果仍然超出,进一步缩小
if (newTextWidth > containerWidth * 0.9 || newTextHeight > containerHeight * 0.9) {
const correctionScale = Math.min(
(containerWidth * 0.9) / newTextWidth,
(containerHeight * 0.9) / newTextHeight
);
timerText.style.fontSize = (finalFontSize * correctionScale) + 'vmin';
}
}, 10);
});
}
// 重置计时器
function resetTimers() {
isRunning = false;
elapsedTime = 0;
if (isCountdownMode && countdownTime > 0) {
updateTimerDisplay(countdownTime);
} else {
timer1Element.textContent = '00:00.00';
timer2Element.textContent = '00:00.00';
}
clearInterval(timerInterval);
timer1Element.classList.remove('blinking');
timer2Element.classList.remove('blinking');
}
// 切换计时器状态
function toggleTimer() {
if (!isRunning) {
// 开始计时
if (isCountdownMode && countdownTime <= 0) {
// 倒计时模式下没有设置时间,不开始
return;
}
startTime = Date.now() - elapsedTime;
isRunning = true;
startTimer();
} else {
// 停止计时
isRunning = false;
clearInterval(timerInterval);
}
}
// 更新计时器显示
function updateTimerDisplay(time) {
const formatted = formatTime(time);
timer1Element.textContent = formatted;
timer2Element.textContent = formatted;
}
// 计时器
function startTimer() {
timerInterval = setInterval(() => {
if (isCountdownMode) {
elapsedTime = Date.now() - startTime;
const remaining = countdownTime - elapsedTime;
if (remaining <= 0) {
clearInterval(timerInterval);
timer1Element.textContent = '00:00.00';
timer2Element.textContent = '00:00.00';
isRunning = false;
handleTimerEnd();
return;
}
// 5秒前提醒
if (remaining <= 5000) {
playAlertSound();
timer1Element.classList.add('blinking');
timer2Element.classList.add('blinking');
}
updateTimerDisplay(remaining);
} else {
elapsedTime = Date.now() - startTime;
updateTimerDisplay(elapsedTime);
}
}, 10);
}
// 处理计时结束
function handleTimerEnd() {
// 播放提示音
playAlertSound();
// 振动(如果支持)
if (navigator.vibrate) {
navigator.vibrate([200, 100, 200, 100, 200]);
}
// 3秒后停止闪烁
setTimeout(() => {
timer1Element.classList.remove('blinking');
timer2Element.classList.remove('blinking');
}, 3000);
}
// 播放提示音
function playAlertSound() {
try {
const context = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = context.createOscillator();
const gainNode = context.createGain();
oscillator.connect(gainNode);
gainNode.connect(context.destination);
oscillator.type = 'sine';
oscillator.frequency.value = 800;
gainNode.gain.value = 0.3;
oscillator.start();
gainNode.gain.exponentialRampToValueAtTime(0.00001, context.currentTime + 0.5);
oscillator.stop(context.currentTime + 0.5);
} catch (e) {
console.log('音频播放失败:', e);
}
}
// 格式化时间为MM:SS.MM
function formatTime(time) {
const totalSeconds = Math.floor(time / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
const centiseconds = Math.floor((time % 1000) / 10);
return `${pad(minutes)}:${pad(seconds)}.${pad(centiseconds)}`;
}
function pad(number) {
return number.toString().padStart(2, '0');
}
// 初始化应用
init();
</script>
</body>
</html>