-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBmiCalculator.html
More file actions
347 lines (310 loc) · 10.5 KB
/
Copy pathBmiCalculator.html
File metadata and controls
347 lines (310 loc) · 10.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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BMI计算器 - 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>
.bmi-calculator {
max-width: 600px;
margin: 0 auto;
}
.input-group {
margin: 15px 0;
}
.input-group label {
display: block;
margin-bottom: 5px;
color: var(--accent-color);
}
.input-group input, .input-group select {
width: 100%;
padding: 8px;
}
.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;
}
.bmi-result {
text-align: center;
padding: 20px;
margin: 20px 0;
border-radius: var(--border-radius);
font-size: 1.5em;
font-weight: bold;
}
.bmi-category-underweight { background-color: #5dade2; } /* 蓝色 - 体重不足 */
.bmi-category-normal { background-color: #58d68d; } /* 绿色 - 正常 */
.bmi-category-overweight { background-color: #f4d03f; } /* 黄色 - 超重 */
.bmi-category-obese { background-color: #e74c3c; } /* 红色 - 肥胖 */
.bmi-chart {
margin-top: 20px;
padding: 15px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
}
.chart-item {
display: flex;
justify-content: space-between;
padding: 8px;
margin: 5px 0;
border-radius: 4px;
}
.chart-underweight { background-color: #5dade2; }
.chart-normal { background-color: #58d68d; }
.chart-overweight { background-color: #f4d03f; }
.chart-obese { background-color: #e74c3c; }
.health-tips {
margin-top: 20px;
padding: 15px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
}
.tip-item {
margin: 10px 0;
padding: 10px;
background-color: var(--bg-secondary);
border-radius: 4px;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section bmi-calculator">
<h2>BMI计算器</h2>
<p>计算身体质量指数(BMI)并评估健康状况</p>
<div class="info-box">
<p><strong>BMI简介:</strong></p>
<p>BMI(Body Mass Index,身体质量指数)是国际上常用的衡量人体胖瘦程度及是否健康的一个标准。</p>
<p>计算公式:BMI = 体重(kg) ÷ 身高²(m)</p>
</div>
<div>
<div class="input-group">
<label for="unitSystem">单位系统</label>
<select id="unitSystem" v-model="unitSystem">
<option value="metric">公制 (公斤/米)</option>
<option value="imperial">英制 (磅/英寸)</option>
</select>
</div>
<div class="input-group">
<label for="height">身高</label>
<input
type="number"
id="height"
v-model.number="height"
:placeholder="unitSystem === 'metric' ? '身高 (厘米)' : '身高 (英寸)'"
min="0"
step="any"
>
</div>
<div class="input-group">
<label for="weight">体重</label>
<input
type="number"
id="weight"
v-model.number="weight"
:placeholder="unitSystem === 'metric' ? '体重 (公斤)' : '体重 (磅)'"
min="0"
step="any"
>
</div>
<div class="controls">
<button @click="calculateBMI">计算BMI</button>
<button @click="clearInputs">清空</button>
</div>
<div class="bmi-result" v-if="bmiValue !== null" :class="bmiCategoryClass">
<p>您的BMI: {{ bmiValue }}</p>
<p>{{ bmiCategory }}</p>
</div>
<div class="bmi-chart">
<h3>BMI分类标准</h3>
<div class="chart-item chart-underweight">
<span>体重不足: < 18.5</span>
</div>
<div class="chart-item chart-normal">
<span>正常: 18.5 - 24.9</span>
</div>
<div class="chart-item chart-overweight">
<span>超重: 25 - 29.9</span>
</div>
<div class="chart-item chart-obese">
<span>肥胖: ≥ 30</span>
</div>
</div>
<div class="health-tips" v-if="bmiValue !== null">
<h3>健康建议</h3>
<div class="tip-item" v-if="bmiCategory === '体重不足'">
<p>您的体重偏轻,建议:</p>
<ul>
<li>适当增加营养摄入,多吃富含蛋白质的食物</li>
<li>规律饮食,一日三餐定时定量</li>
<li>适量运动,增强体质</li>
<li>如有需要,咨询营养师或医生</li>
</ul>
</div>
<div class="tip-item" v-if="bmiCategory === '正常'">
<p>恭喜!您的体重在正常范围内,请继续保持:</p>
<ul>
<li>均衡饮食,保证各类营养素摄入</li>
<li>坚持规律运动</li>
<li>保持良好的生活习惯</li>
<li>定期体检监测健康状况</li>
</ul>
</div>
<div class="tip-item" v-if="bmiCategory === '超重'">
<p>您的体重超重,建议:</p>
<ul>
<li>控制饮食,减少高热量食物摄入</li>
<li>增加体育锻炼,每周至少150分钟中等强度运动</li>
<li>制定合理的减重计划</li>
<li>必要时寻求专业指导</li>
</ul>
</div>
<div class="tip-item" v-if="bmiCategory === '肥胖'">
<p>您的体重属于肥胖范围,强烈建议:</p>
<ul>
<li>立即采取行动,调整饮食结构</li>
<li>大幅增加身体活动量</li>
<li>寻求医生或营养师的专业帮助</li>
<li>设定现实可行的减重目标</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const unitSystem = ref('metric'); // 'metric' 或 'imperial'
const height = ref('');
const weight = ref('');
const bmiValue = ref(null);
const bmiCategory = ref('');
// 计算BMI
const calculateBMI = () => {
if (!height.value || !weight.value || height.value <= 0 || weight.value <= 0) {
alert('请输入有效的身高和体重');
return;
}
let heightInMeters, weightInKg;
if (unitSystem.value === 'metric') {
// 公制:身高厘米转米,体重公斤
heightInMeters = height.value / 100;
weightInKg = weight.value;
} else {
// 英制:身高英寸转米,体重磅转公斤
heightInMeters = height.value * 0.0254; // 1英寸 = 0.0254米
weightInKg = weight.value * 0.453592; // 1磅 = 0.453592公斤
}
// 计算BMI
const bmi = weightInKg / (heightInMeters * heightInMeters);
bmiValue.value = bmi.toFixed(1);
// 确定BMI分类
if (bmi < 18.5) {
bmiCategory.value = '体重不足';
} else if (bmi < 25) {
bmiCategory.value = '正常';
} else if (bmi < 30) {
bmiCategory.value = '超重';
} else {
bmiCategory.value = '肥胖';
}
};
// BMI分类对应的CSS类
const bmiCategoryClass = computed(() => {
if (!bmiCategory.value) return '';
switch(bmiCategory.value) {
case '体重不足':
return 'bmi-category-underweight';
case '正常':
return 'bmi-category-normal';
case '超重':
return 'bmi-category-overweight';
case '肥胖':
return 'bmi-category-obese';
default:
return '';
}
});
// 清空输入
const clearInputs = () => {
height.value = '';
weight.value = '';
bmiValue.value = null;
bmiCategory.value = '';
};
return {
unitSystem,
height,
weight,
bmiValue,
bmiCategory,
bmiCategoryClass,
calculateBMI,
clearInputs
};
}
}).mount('#app');
</script>
</body>
</html>