-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitConverter.html
More file actions
402 lines (358 loc) · 13.5 KB
/
Copy pathUnitConverter.html
File metadata and controls
402 lines (358 loc) · 13.5 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
<!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>
.converter-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 20px 0;
}
.converter-section {
display: flex;
flex-direction: column;
}
.converter-section input, .converter-section select {
padding: 8px;
margin-top: 5px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.conversion-history {
margin-top: 20px;
padding: 15px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
}
.history-item {
padding: 5px 0;
border-bottom: 1px solid var(--bg-secondary);
}
.history-item:last-child {
border-bottom: none;
}
@media (max-width: 768px) {
.converter-container {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>单位换算器</h2>
<p>常用单位之间的换算</p>
<div class="info-box">
<p><strong>支持的换算类型:</strong></p>
<p>• 长度:米、千米、厘米、毫米、英寸、英尺、码、英里</p>
<p>• 重量:克、千克、吨、盎司、磅</p>
<p>• 温度:摄氏度、华氏度、开尔文</p>
<p>• 面积:平方米、平方千米、公顷、英亩、平方英尺、平方英里</p>
<p>• 体积:毫升、升、立方厘米、立方米、加仑、品脱、夸脱</p>
<p>• 时间:毫秒、秒、分钟、小时、天、周、月、年</p>
</div>
<div class="converter-container">
<div class="converter-section">
<label for="conversionType">换算类型</label>
<select id="conversionType" v-model="conversionType" @change="resetValues">
<option value="length">长度</option>
<option value="weight">重量</option>
<option value="temperature">温度</option>
<option value="area">面积</option>
<option value="volume">体积</option>
<option value="time">时间</option>
</select>
<label for="fromUnit">从</label>
<select id="fromUnit" v-model="fromUnit">
<option v-for="unit in units[conversionType]" :value="unit.key">{{ unit.name }}</option>
</select>
<label for="inputValue">数值</label>
<input
type="number"
id="inputValue"
v-model.number="inputValue"
@input="convertValue"
placeholder="输入数值"
>
</div>
<div class="converter-section">
<label for="toUnit">转换为</label>
<select id="toUnit" v-model="toUnit" @change="convertValue">
<option v-for="unit in units[conversionType]" :value="unit.key">{{ unit.name }}</option>
</select>
<label>结果</label>
<div class="result" style="padding: 10px; margin-top: 5px;">
<p>{{ result }}</p>
</div>
</div>
</div>
<div class="controls">
<button @click="convertValue">立即换算</button>
<button @click="switchUnits">切换单位</button>
<button @click="clearValues">清空</button>
</div>
<div class="conversion-history" v-if="history.length > 0">
<h3>换算历史</h3>
<div v-for="(item, index) in history" :key="index" class="history-item">
{{ item.fromValue }} {{ item.fromUnitName }} = {{ item.toValue }} {{ item.toUnitName }}
</div>
<button @click="clearHistory" style="margin-top: 10px;">清空历史</button>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, reactive, computed } = Vue;
createApp({
setup() {
const conversionType = ref('length');
const fromUnit = ref('meter');
const toUnit = ref('kilometer');
const inputValue = ref(1);
const result = ref('');
const history = ref([]);
// 定义单位换算关系
const units = {
length: [
{ key: 'millimeter', name: '毫米 (mm)', factor: 0.001 },
{ key: 'centimeter', name: '厘米 (cm)', factor: 0.01 },
{ key: 'meter', name: '米 (m)', factor: 1 },
{ key: 'kilometer', name: '千米 (km)', factor: 1000 },
{ key: 'inch', name: '英寸 (in)', factor: 0.0254 },
{ key: 'foot', name: '英尺 (ft)', factor: 0.3048 },
{ key: 'yard', name: '码 (yd)', factor: 0.9144 },
{ key: 'mile', name: '英里 (mi)', factor: 1609.344 }
],
weight: [
{ key: 'milligram', name: '毫克 (mg)', factor: 0.000001 },
{ key: 'gram', name: '克 (g)', factor: 0.001 },
{ key: 'kilogram', name: '千克 (kg)', factor: 1 },
{ key: 'ton', name: '吨 (t)', factor: 1000 },
{ key: 'ounce', name: '盎司 (oz)', factor: 0.0283495 },
{ key: 'pound', name: '磅 (lb)', factor: 0.453592 }
],
temperature: [
{ key: 'celsius', name: '摄氏度 (°C)' },
{ key: 'fahrenheit', name: '华氏度 (°F)' },
{ key: 'kelvin', name: '开尔文 (K)' }
],
area: [
{ key: 'squareMillimeter', name: '平方毫米 (mm²)', factor: 0.000001 },
{ key: 'squareCentimeter', name: '平方厘米 (cm²)', factor: 0.0001 },
{ key: 'squareMeter', name: '平方米 (m²)', factor: 1 },
{ key: 'hectare', name: '公顷 (ha)', factor: 10000 },
{ key: 'squareKilometer', name: '平方千米 (km²)', factor: 1000000 },
{ key: 'acre', name: '英亩 (ac)', factor: 4046.86 },
{ key: 'squareFoot', name: '平方英尺 (ft²)', factor: 0.092903 },
{ key: 'squareMile', name: '平方英里 (mi²)', factor: 2589988.11 }
],
volume: [
{ key: 'milliliter', name: '毫升 (mL)', factor: 0.001 },
{ key: 'liter', name: '升 (L)', factor: 1 },
{ key: 'cubicCentimeter', name: '立方厘米 (cm³)', factor: 0.001 },
{ key: 'cubicMeter', name: '立方米 (m³)', factor: 1000 },
{ key: 'gallonUS', name: '加仑(美) (gal)', factor: 3.78541 },
{ key: 'gallonUK', name: '加仑(英) (gal)', factor: 4.54609 },
{ key: 'pintUS', name: '品脱(美) (pt)', factor: 0.473176 },
{ key: 'pintUK', name: '品脱(英) (pt)', factor: 0.568261 },
{ key: 'quartUS', name: '夸脱(美) (qt)', factor: 0.946353 },
{ key: 'quartUK', name: '夸脱(英) (qt)', factor: 1.13652 }
],
time: [
{ key: 'millisecond', name: '毫秒 (ms)', factor: 0.001 },
{ key: 'second', name: '秒 (s)', factor: 1 },
{ key: 'minute', name: '分钟 (min)', factor: 60 },
{ key: 'hour', name: '小时 (hr)', factor: 3600 },
{ key: 'day', name: '天 (d)', factor: 86400 },
{ key: 'week', name: '周 (wk)', factor: 604800 },
{ key: 'month', name: '月 (mo)', factor: 2628000 }, // 平均月份 (30.44天)
{ key: 'year', name: '年 (yr)', factor: 31536000 } // 平年
]
};
// 获取单位详情
const getUnitDetails = (type, unitKey) => {
return units[type].find(unit => unit.key === unitKey);
};
// 执行换算
const convertValue = () => {
if (inputValue.value === '' || isNaN(inputValue.value)) {
result.value = '请输入有效数值';
return;
}
if (conversionType.value === 'temperature') {
// 温度换算需要特殊处理
result.value = convertTemperature(inputValue.value, fromUnit.value, toUnit.value);
} else {
// 其他单位换算
const fromUnitInfo = getUnitDetails(conversionType.value, fromUnit.value);
const toUnitInfo = getUnitDetails(conversionType.value, toUnit.value);
if (!fromUnitInfo || !toUnitInfo) {
result.value = '无效的单位';
return;
}
// 转换为标准单位(以factor为基准),然后再转换为目标单位
const standardValue = inputValue.value * fromUnitInfo.factor;
const finalValue = standardValue / toUnitInfo.factor;
result.value = finalValue.toFixed(6);
}
// 添加到历史记录
addToHistory();
};
// 温度换算
const convertTemperature = (value, from, to) => {
// 先转换为摄氏度
let celsius;
switch(from) {
case 'celsius':
celsius = value;
break;
case 'fahrenheit':
celsius = (value - 32) * 5/9;
break;
case 'kelvin':
celsius = value - 273.15;
break;
default:
return '无效的温度单位';
}
// 从摄氏度转换为目标单位
let result;
switch(to) {
case 'celsius':
result = celsius;
break;
case 'fahrenheit':
result = (celsius * 9/5) + 32;
break;
case 'kelvin':
result = celsius + 273.15;
break;
default:
return '无效的目标温度单位';
}
return result.toFixed(6);
};
// 添加到历史记录
const addToHistory = () => {
if (inputValue.value !== '' && !isNaN(inputValue.value) && result.value && !isNaN(result.value)) {
const fromUnitInfo = getUnitDetails(conversionType.value, fromUnit.value);
const toUnitInfo = getUnitDetails(conversionType.value, toUnit.value);
history.value.unshift({
fromValue: parseFloat(inputValue.value).toFixed(6),
toValue: parseFloat(result.value).toFixed(6),
fromUnitName: fromUnitInfo.name,
toUnitName: toUnitInfo.name
});
// 限制历史记录数量为10条
if (history.value.length > 10) {
history.value.pop();
}
}
};
// 切换单位
const switchUnits = () => {
const temp = fromUnit.value;
fromUnit.value = toUnit.value;
toUnit.value = temp;
convertValue();
};
// 清空数值
const clearValues = () => {
inputValue.value = '';
result.value = '';
};
// 重置数值
const resetValues = () => {
inputValue.value = 1;
fromUnit.value = units[conversionType.value][0].key;
toUnit.value = units[conversionType.value][1].key;
convertValue();
};
// 清空历史记录
const clearHistory = () => {
history.value = [];
};
// 初始化换算
resetValues();
return {
conversionType,
fromUnit,
toUnit,
inputValue,
result,
history,
units,
convertValue,
switchUnits,
clearValues,
resetValues,
clearHistory
};
}
}).mount('#app');
</script>
</body>
</html>