-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountdownTimer.html
More file actions
366 lines (321 loc) · 10 KB
/
Copy pathCountdownTimer.html
File metadata and controls
366 lines (321 loc) · 10 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>倒计时器 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.countdown-timer {
max-width: 600px;
margin: 0 auto;
text-align: center;
}
.timer-display {
font-size: 3em;
font-weight: bold;
margin: 20px 0;
padding: 20px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
letter-spacing: 3px;
}
.controls {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 10px;
margin: 20px 0;
}
.time-inputs {
display: flex;
justify-content: center;
gap: 10px;
margin: 20px 0;
flex-wrap: wrap;
}
.time-input {
display: flex;
flex-direction: column;
align-items: center;
}
.time-input input {
width: 80px;
padding: 8px;
text-align: center;
}
.time-input label {
margin-top: 5px;
font-size: 0.9em;
color: var(--accent-color);
}
.status {
margin: 15px 0;
font-weight: bold;
}
.finished {
color: #2ecc71;
}
.running {
color: #3498db;
}
.paused {
color: #f39c12;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.presets {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 10px;
margin: 15px 0;
}
.preset-btn {
padding: 10px;
background-color: var(--bg-secondary);
border: none;
border-radius: var(--border-radius);
cursor: pointer;
}
.preset-btn:hover {
background-color: var(--bg-tertiary);
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section countdown-timer">
<h2>倒计时器</h2>
<p>设置并启动倒计时</p>
<div class="info-box">
<p><strong>使用说明:</strong></p>
<p>• 设置时间后点击"开始倒计时"</p>
<p>• 可以暂停、恢复或重置倒计时</p>
<p>• 使用预设快速设置常用时间</p>
</div>
<div>
<div class="timer-display">{{ formattedTime }}</div>
<div class="status" :class="statusClass">
{{ statusText }}
</div>
<div class="time-inputs">
<div class="time-input">
<input
type="number"
v-model.number="hours"
min="0"
max="23"
:disabled="isActive"
>
<label>小时</label>
</div>
<div class="time-input">
<input
type="number"
v-model.number="minutes"
min="0"
max="59"
:disabled="isActive"
>
<label>分钟</label>
</div>
<div class="time-input">
<input
type="number"
v-model.number="seconds"
min="0"
max="59"
:disabled="isActive"
>
<label>秒</label>
</div>
</div>
<div class="controls">
<button @click="startTimer" :disabled="isActive">开始</button>
<button @click="pauseTimer" :disabled="!isActive || isFinished">暂停</button>
<button @click="resumeTimer" :disabled="!isPaused">恢复</button>
<button @click="resetTimer">重置</button>
</div>
<div class="presets">
<button class="preset-btn" @click="setPreset(5, 0, 0)">5分钟</button>
<button class="preset-btn" @click="setPreset(10, 0, 0)">10分钟</button>
<button class="preset-btn" @click="setPreset(15, 0, 0)">15分钟</button>
<button class="preset-btn" @click="setPreset(30, 0, 0)">30分钟</button>
<button class="preset-btn" @click="setPreset(0, 1, 0)">1分钟</button>
<button class="preset-btn" @click="setPreset(0, 30, 0)">30秒</button>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const hours = ref(0);
const minutes = ref(5);
const seconds = ref(0);
const totalSeconds = ref(0);
const isActive = ref(false);
const isPaused = ref(false);
const isFinished = ref(false);
const timerInterval = ref(null);
// 格式化时间显示
const formattedTime = computed(() => {
const hrs = Math.floor(totalSeconds.value / 3600);
const mins = Math.floor((totalSeconds.value % 3600) / 60);
const secs = totalSeconds.value % 60;
return `${hrs.toString().padStart(2, '0')}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
});
// 状态文本
const statusText = computed(() => {
if (isFinished.value) return '已完成!';
if (isActive.value && !isPaused.value) return '运行中...';
if (isPaused.value) return '已暂停';
return '待开始';
});
// 状态类
const statusClass = computed(() => {
if (isFinished.value) return 'finished';
if (isActive.value && !isPaused.value) return 'running';
if (isPaused.value) return 'paused';
return '';
});
// 开始倒计时
const startTimer = () => {
if (totalSeconds.value <= 0) {
totalSeconds.value = hours.value * 3600 + minutes.value * 60 + seconds.value;
if (totalSeconds.value <= 0) {
alert('请设置大于0的时间');
return;
}
}
isActive.value = true;
isPaused.value = false;
isFinished.value = false;
timerInterval.value = setInterval(() => {
totalSeconds.value--;
if (totalSeconds.value <= 0) {
clearInterval(timerInterval.value);
totalSeconds.value = 0;
isActive.value = false;
isPaused.value = false;
isFinished.value = true;
// 浏览器通知
if (Notification.permission === 'granted') {
new Notification('倒计时结束!', {
body: '您设置的倒计时已经完成。',
icon: 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">⏰</text></svg>'
});
}
}
}, 1000);
};
// 暂停倒计时
const pauseTimer = () => {
if (isActive.value && !isPaused.value) {
clearInterval(timerInterval.value);
isPaused.value = true;
isActive.value = false;
}
};
// 恢复倒计时
const resumeTimer = () => {
if (isPaused.value && totalSeconds.value > 0) {
startTimer();
}
};
// 重置倒计时
const resetTimer = () => {
clearInterval(timerInterval.value);
isActive.value = false;
isPaused.value = false;
isFinished.value = false;
totalSeconds.value = hours.value * 3600 + minutes.value * 60 + seconds.value;
};
// 设置预设时间
const setPreset = (h, m, s) => {
if (!isActive.value) {
hours.value = h;
minutes.value = m;
seconds.value = s;
totalSeconds.value = h * 3600 + m * 60 + s;
}
};
// 请求通知权限
if (Notification.permission === 'default') {
Notification.requestPermission();
}
return {
hours,
minutes,
seconds,
formattedTime,
statusText,
statusClass,
isActive,
isPaused,
isFinished,
startTimer,
pauseTimer,
resumeTimer,
resetTimer,
setPreset
};
}
}).mount('#app');
</script>
</body>
</html>