BMI / 卡路里计算器

Setang 23 阅读 信息分享

本地运行,HTML页面

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>BMI & 卡路里计算器</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&family=Playfair+Display:wght@700&display=swap');

        :root {
            --primary: #6d28d9;
            --accent: #f472b6;
            --bg: #0f172a;
            --card: #1e2937;
            --text: #e2e8f0;
            --text-light: #94a3b8;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans SC', system-ui, sans-serif;
            background: linear-gradient(135deg, #0f172a 0%, #1e2937 100%);
            color: var(--text);
            min-height: 100vh;
            display: flex;
            align-items: flex-start;
            justify-content: center;
            padding: 16px 12px;
            overflow-y: auto;
            -webkit-overflow-scrolling: touch;
            position: relative;
        }

        .particles {
            position: fixed;
            top: 0; left: 0; width: 100%; height: 100%;
            pointer-events: none;
            z-index: 1;
            overflow: hidden;
        }

        .container {
            position: relative;
            z-index: 10;
            width: 100%;
            max-width: 520px;
            margin: 0 auto;
        }

        .header {
            text-align: center;
            margin-bottom: 24px;
        }

        .title {
            font-family: 'Playfair Display', serif;
            font-size: clamp(2.2rem, 6vw, 3rem);
            font-weight: 700;
            background: linear-gradient(90deg, #c4b5fd, #f472b6);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 6px;
        }

        .subtitle {
            color: var(--text-light);
            font-size: 1rem;
        }

        .card {
            background: var(--card);
            border-radius: 28px;
            padding: 32px 24px;
            box-shadow: 0 20px 40px -12px rgba(0, 0, 0, 0.5);
            border: 1px solid rgba(167, 139, 250, 0.15);
            margin-bottom: 20px;
        }

        .tabs {
            display: flex;
            background: rgba(15, 23, 42, 0.7);
            border-radius: 9999px;
            padding: 5px;
            margin-bottom: 28px;
        }

        .tab {
            flex: 1;
            padding: 14px 0;
            text-align: center;
            border-radius: 9999px;
            font-weight: 600;
            cursor: pointer;
            font-size: 1.02rem;
            transition: all 0.3s ease;
        }

        .tab.active {
            background: linear-gradient(90deg, var(--primary), var(--accent));
            color: white;
            box-shadow: 0 4px 15px rgba(109, 40, 217, 0.4);
        }

        .input-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: var(--text-light);
            font-size: 0.95rem;
        }

        input, select {
            width: 100%;
            background: rgba(15, 23, 42, 0.75);
            border: 2px solid rgba(148, 163, 184, 0.35);
            border-radius: 16px;
            padding: 15px 18px;
            color: var(--text);
            font-size: 1.1rem;
            transition: all 0.3s;
        }

        input:focus, select:focus {
            border-color: var(--accent);
            box-shadow: 0 0 0 4px rgba(244, 114, 182, 0.2);
            outline: none;
        }

        .result {
            margin-top: 28px;
            padding: 24px;
            background: rgba(15, 23, 42, 0.65);
            border-radius: 24px;
            border: 2px solid rgba(167, 139, 250, 0.25);
            text-align: center;
        }

        .result-value {
            font-size: clamp(2.6rem, 8vw, 3.4rem);
            font-weight: 700;
            background: linear-gradient(90deg, #c4b5fd, #f472b6);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            line-height: 1.1;
        }

        .result-label {
            font-size: 1.05rem;
            color: var(--text-light);
            margin: 8px 0 16px;
        }

        .bmi-category {
            padding: 10px 24px;
            border-radius: 9999px;
            font-weight: 600;
            display: inline-block;
            font-size: 1.05rem;
        }

        .btn {
            width: 100%;
            padding: 17px;
            font-size: 1.22rem;
            font-weight: 700;
            background: linear-gradient(90deg, #6d28d9, #c026d3);
            color: white;
            border: none;
            border-radius: 9999px;
            cursor: pointer;
            margin-top: 16px;
            transition: all 0.4s ease;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 12px 25px -8px #c026d3;
        }

        .info {
            font-size: 0.88rem;
            color: var(--text-light);
            line-height: 1.55;
            padding: 0 8px;
        }

        .footer {
            text-align: center;
            margin-top: 32px;
            color: #64748b;
            font-size: 0.82rem;
        }

        /* 移动端优化 */
        @media (max-width: 480px) {
            body {
                padding: 12px 8px;
            }
            .card {
                padding: 28px 20px;
            }
        }
    </style>
</head>
<body>
    <div class="particles" id="particles"></div>

    <div class="container">
        <div class="header">
            <div class="title">健康计算器</div>
            <div class="subtitle">BMI • 基础代谢 • 每日卡路里</div>
        </div>

        <div class="card">
            <div class="tabs">
                <div class="tab active" data-tab="bmi">BMI 计算</div>
                <div class="tab" data-tab="calorie">卡路里计算</div>
            </div>

            <!-- BMI 面板 -->
            <div id="bmi-panel">
                <div class="input-group">
                    <label>性别</label>
                    <select id="gender">
                        <option value="male">男</option>
                        <option value="female">女</option>
                    </select>
                </div>

                <div class="input-group">
                    <label>年龄(岁)</label>
                    <input type="number" id="age" value="28" min="10" max="100">
                </div>

                <div class="input-group">
                    <label>身高(cm)</label>
                    <input type="number" id="height" value="170" min="100" max="250">
                </div>

                <div class="input-group">
                    <label>体重(kg)</label>
                    <input type="number" id="weight" value="65" min="30" max="300" step="0.1">
                </div>

                <button class="btn" id="calcBMI">计算 BMI</button>

                <div class="result" id="bmiResult" style="display: none;">
                    <div class="result-value" id="bmiValue">22.5</div>
                    <div class="result-label">你的 BMI 值</div>
                    <div class="bmi-category" id="bmiCategory">正常范围</div>
                </div>
            </div>

            <!-- 卡路里面板 -->
            <div id="calorie-panel" style="display: none;">
                <div class="input-group">
                    <label>性别</label>
                    <select id="gender2">
                        <option value="male">男</option>
                        <option value="female">女</option>
                    </select>
                </div>

                <div class="input-group">
                    <label>年龄(岁)</label>
                    <input type="number" id="age2" value="28" min="10" max="100">
                </div>

                <div class="input-group">
                    <label>身高(cm)</label>
                    <input type="number" id="height2" value="170" min="100" max="250">
                </div>

                <div class="input-group">
                    <label>体重(kg)</label>
                    <input type="number" id="weight2" value="65" min="30" max="300" step="0.1">
                </div>

                <div class="input-group">
                    <label>活动水平</label>
                    <select id="activity">
                        <option value="1.2">久坐(几乎不运动)</option>
                        <option value="1.375" selected>轻度活动(每周1-3天)</option>
                        <option value="1.55">中度活动(每周3-5天)</option>
                        <option value="1.725">重度活动(每周6-7天)</option>
                        <option value="1.9">极重度活动(运动员/重体力)</option>
                    </select>
                </div>

                <button class="btn" id="calcCalorie">计算每日需求</button>

                <div class="result" id="calorieResult" style="display: none;">
                    <div style="font-size: 1rem; color: var(--text-light);">基础代谢率 BMR</div>
                    <div class="result-value" id="bmrValue">1580</div>
                    <div style="margin: 16px 0 8px; color: var(--text-light);">每日总热量消耗 TDEE</div>
                    <div class="result-value" id="tdeeValue" style="font-size: 2.7rem;">2180</div>
                    <div class="result-label">kcal / 天</div>
                </div>
            </div>
        </div>

        <div class="info">
            <strong>说明:</strong><br>
            • BMI 是参考指标,不能完全代表健康状况<br>
            • 卡路里使用 Harris-Benedict 公式计算<br>
            • 建议结合体脂率等其他指标综合判断
        </div>

        <div class="footer">
            纯本地运行 • 数据不上传 • 祝你健康!
        </div>
    </div>

    <script>
        // 粒子背景
        function createParticles() {
            const container = document.getElementById('particles');
            for (let i = 0; i < 40; i++) {
                const p = document.createElement('div');
                const size = Math.random() * 5 + 3;
                p.style.position = 'absolute';
                p.style.width = `${size}px`;
                p.style.height = `${size}px`;
                p.style.background = 'rgba(167, 139, 250, 0.3)';
                p.style.borderRadius = '50%';
                p.style.left = `${Math.random() * 100}vw`;
                p.style.top = `${Math.random() * 120}vh`;
                p.style.opacity = Math.random() * 0.5 + 0.2;
                p.style.animation = `float ${Math.random() * 25 + 18}s linear infinite`;
                container.appendChild(p);
            }
        }

        const style = document.createElement('style');
        style.innerHTML = `
            @keyframes float {
                0% { transform: translateY(0) rotate(0deg); }
                100% { transform: translateY(-150vh) rotate(720deg); }
            }
        `;
        document.head.appendChild(style);

        // 切换面板
        document.querySelectorAll('.tab').forEach(tab => {
            tab.addEventListener('click', () => {
                document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
                tab.classList.add('active');

                const isBMI = tab.dataset.tab === 'bmi';
                document.getElementById('bmi-panel').style.display = isBMI ? 'block' : 'none';
                document.getElementById('calorie-panel').style.display = isBMI ? 'none' : 'block';
            });
        });

        // BMI 计算
        function calculateBMI() {
            const height = parseFloat(document.getElementById('height').value);
            const weight = parseFloat(document.getElementById('weight').value);

            if (!height || !weight || height <= 0 || weight <= 0) {
                alert('请输入有效的身高和体重');
                return;
            }

            const bmi = (weight / ((height / 100) ** 2)).toFixed(1);
            const resultDiv = document.getElementById('bmiResult');
            const valueEl = document.getElementById('bmiValue');
            const categoryEl = document.getElementById('bmiCategory');

            valueEl.textContent = bmi;
            resultDiv.style.display = 'block';

            let text = '', color = '';
            if (bmi < 18.5) { text = '体重偏轻'; color = '#60a5fa'; }
            else if (bmi < 24) { text = '正常范围'; color = '#4ade80'; }
            else if (bmi < 28) { text = '超重'; color = '#fbbf24'; }
            else { text = '肥胖'; color = '#f87171'; }

            categoryEl.textContent = text;
            categoryEl.style.backgroundColor = color + '30';
            categoryEl.style.color = color;
        }

        // 卡路里计算
        function calculateCalorie() {
            const gender = document.getElementById('gender2').value;
            const age = parseFloat(document.getElementById('age2').value);
            const height = parseFloat(document.getElementById('height2').value);
            const weight = parseFloat(document.getElementById('weight2').value);
            const activity = parseFloat(document.getElementById('activity').value);

            if (!age || !height || !weight || age <= 0 || height <= 0 || weight <= 0) {
                alert('请填写完整且有效的信息');
                return;
            }

            let bmr = gender === 'male' 
                ? 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age)
                : 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age);

            const tdee = bmr * activity;

            document.getElementById('bmrValue').textContent = Math.round(bmr);
            document.getElementById('tdeeValue').textContent = Math.round(tdee);
            document.getElementById('calorieResult').style.display = 'block';
        }

        // 按钮事件
        document.getElementById('calcBMI').addEventListener('click', calculateBMI);
        document.getElementById('calcCalorie').addEventListener('click', calculateCalorie);

        // 回车键支持
        document.addEventListener('keydown', e => {
            if (e.key === 'Enter') {
                if (document.getElementById('bmi-panel').style.display !== 'none') {
                    calculateBMI();
                } else {
                    calculateCalorie();
                }
            }
        });

        // 初始化
        window.onload = () => {
            createParticles();
        };
    </script>
</body>
</html>