← Kapat
body-fat-calculator.js
// Vücut Yağ Analizi JavaScript
document.addEventListener('DOMContentLoaded', function() {
let currentGender = 'male';
// DOM elementlerini al
const heightSlider = document.getElementById('bodyFat-heightSlider');
const heightInput = document.getElementById('bodyFat-heightInput');
const weightSlider = document.getElementById('bodyFat-weightSlider');
const weightInput = document.getElementById('bodyFat-weightInput');
const waistSlider = document.getElementById('waistSlider');
const waistInput = document.getElementById('waistInput');
const neckSlider = document.getElementById('neckSlider');
const neckInput = document.getElementById('neckInput');
const bodyFatResult = document.getElementById('bodyFatResult');
const bodyFatCategory = document.getElementById('bodyFatCategory');
// Cinsiyet seçimi
window.bodyFatSelectGender = function(gender) {
currentGender = gender;
// Butonları güncelle
const buttons = document.querySelectorAll('.bodyFat-gender-btn');
buttons.forEach(btn => btn.classList.remove('active'));
// Aktif butonu seç
const activeButton = [...buttons].find(btn =>
btn.getAttribute('data-gender') === gender
);
if (activeButton) {
activeButton.classList.add('active');
}
// Hesaplamayı yeniden yap
calculateBodyFat();
};
// Slider background güncelleme
function updateSliderBackground(slider) {
const min = parseFloat(slider.min);
const max = parseFloat(slider.max);
const value = parseFloat(slider.value);
const percentage = ((value - min) / (max - min)) * 100;
slider.style.background = `linear-gradient(to right, #ff8c00 0%, #ff8c00 ${percentage}%, #3f3f46 ${percentage}%, #3f3f46 100%)`;
}
// Slider ve input senkronizasyonu
function syncSliderAndInput(slider, input) {
if (!slider || !input) return;
slider.addEventListener('input', function() {
input.value = this.value;
updateSliderBackground(this);
calculateBodyFat();
});
input.addEventListener('input', function() {
const value = parseFloat(this.value);
const min = parseFloat(slider.min);
const max = parseFloat(slider.max);
if (value >= min && value <= max) {
slider.value = this.value;
updateSliderBackground(slider);
}
calculateBodyFat();
});
}
// Tüm slider-input çiftlerini senkronize et
syncSliderAndInput(heightSlider, heightInput);
syncSliderAndInput(weightSlider, weightInput);
syncSliderAndInput(waistSlider, waistInput);
syncSliderAndInput(neckSlider, neckInput);
// Slider background'larını başlangıçta ayarla
if (heightSlider) updateSliderBackground(heightSlider);
if (weightSlider) updateSliderBackground(weightSlider);
if (waistSlider) updateSliderBackground(waistSlider);
if (neckSlider) updateSliderBackground(neckSlider);
// Vücut yağ oranı hesaplama
function calculateBodyFat() {
const height = parseFloat(heightInput?.value || 0);
const weight = parseFloat(weightInput?.value || 0);
const waist = parseFloat(waistInput?.value || 0);
const neck = parseFloat(neckInput?.value || 0);
// Girdi kontrolü - otomatik hesaplama için uyarı verme
if (!height || !weight || !waist || !neck) {
if (bodyFatResult) bodyFatResult.textContent = '0.0';
if (bodyFatCategory) bodyFatCategory.textContent = 'Tüm değerleri girin';
return;
}
if (height < 140 || height > 220) {
if (bodyFatResult) bodyFatResult.textContent = '0.0';
if (bodyFatCategory) bodyFatCategory.textContent = 'Geçersiz boy değeri';
return;
}
if (weight < 40 || weight > 200) {
if (bodyFatResult) bodyFatResult.textContent = '0.0';
if (bodyFatCategory) bodyFatCategory.textContent = 'Geçersiz kilo değeri';
return;
}
if (waist < 60 || waist > 150) {
if (bodyFatResult) bodyFatResult.textContent = '0.0';
if (bodyFatCategory) bodyFatCategory.textContent = 'Geçersiz bel çevresi';
return;
}
if (neck < 25 || neck > 55) {
if (bodyFatResult) bodyFatResult.textContent = '0.0';
if (bodyFatCategory) bodyFatCategory.textContent = 'Geçersiz boyun çevresi';
return;
}
let bodyFatPercentage;
// BMI hesaplama (kilo etkisini dahil etmek için)
const heightInMeters = height / 100;
const bmi = weight / (heightInMeters * heightInMeters);
if (currentGender === 'male') {
// Erkekler için US Navy formülü
const logValue = waist - neck;
if (logValue <= 0 || height <= 0) {
bodyFatPercentage = 0;
} else {
const navyFormula = 495 / (1.0324 - 0.19077 * Math.log10(logValue) + 0.15456 * Math.log10(height)) - 450;
// BMI tabanlı düzeltme faktörü (erkekler için)
const bmiCorrection = (bmi - 22) * 0.3; // Ideal BMI 22'den sapma
bodyFatPercentage = navyFormula + bmiCorrection;
}
} else {
// Kadınlar için US Navy formülü (kalça yerine bel çevresi kullanıyoruz)
// Kadınlar için: 495 / (1.29579 - 0.35004 * log10(waist + hip - neck) + 0.22100 * log10(height)) - 450
// Hip olmadığı için alternatif formül: waist değerini hip olarak kullanıyoruz
const logValue1 = waist + (waist * 0.85) - neck; // waist * 0.85 hip tahmini
const logValue2 = height;
if (logValue1 <= 0 || logValue2 <= 0) {
bodyFatPercentage = 0;
} else {
const navyFormula = 495 / (1.29579 - 0.35004 * Math.log10(logValue1) + 0.22100 * Math.log10(logValue2)) - 450;
// BMI tabanlı düzeltme faktörü (kadınlar için)
const bmiCorrection = (bmi - 21) * 0.4; // Ideal BMI 21'den sapma
bodyFatPercentage = navyFormula + bmiCorrection;
}
}
// NaN kontrolü ve negatif değerleri sıfırla
if (isNaN(bodyFatPercentage) || bodyFatPercentage < 0) {
bodyFatPercentage = 0;
}
// Sonucu göster
if (bodyFatResult) {
bodyFatResult.textContent = bodyFatPercentage.toFixed(1);
}
// Kategori değerlendirmesi
updateBodyFatCategory(bodyFatPercentage);
}
// Vücut yağ kategorisi değerlendirmesi
function updateBodyFatCategory(bodyFatPercentage) {
let category = '';
let color = '';
if (currentGender === 'male') {
if (bodyFatPercentage >= 2 && bodyFatPercentage <= 5) {
category = 'Temel Yağ';
color = '#3b82f6'; // blue
} else if (bodyFatPercentage >= 6 && bodyFatPercentage <= 13) {
category = 'Atletik';
color = '#10b981'; // green
} else if (bodyFatPercentage >= 14 && bodyFatPercentage <= 17) {
category = 'Fitness';
color = '#eab308'; // yellow
} else if (bodyFatPercentage >= 18 && bodyFatPercentage <= 24) {
category = 'Ortalama';
color = '#f97316'; // orange
} else if (bodyFatPercentage >= 25) {
category = 'Obez';
color = '#ef4444'; // red
} else {
category = 'Çok Düşük';
color = '#6366f1'; // indigo
}
} else {
if (bodyFatPercentage >= 10 && bodyFatPercentage <= 13) {
category = 'Temel Yağ';
color = '#3b82f6'; // blue
} else if (bodyFatPercentage >= 14 && bodyFatPercentage <= 20) {
category = 'Atletik';
color = '#10b981'; // green
} else if (bodyFatPercentage >= 21 && bodyFatPercentage <= 24) {
category = 'Fitness';
color = '#eab308'; // yellow
} else if (bodyFatPercentage >= 25 && bodyFatPercentage <= 31) {
category = 'Ortalama';
color = '#f97316'; // orange
} else if (bodyFatPercentage >= 32) {
category = 'Obez';
color = '#ef4444'; // red
} else {
category = 'Çok Düşük';
color = '#6366f1'; // indigo
}
}
// Kategori bilgisini göster
if (bodyFatCategory) {
bodyFatCategory.textContent = category;
bodyFatCategory.style.color = color;
}
// Sonuç rengini güncelle
if (bodyFatResult) {
bodyFatResult.style.color = color;
}
// Result unit (% işareti) rengini de güncelle
const resultUnit = document.querySelector('.result-unit');
if (resultUnit) {
resultUnit.style.color = color;
}
}
// İlk yükleme sırasında hesapla
calculateBodyFat();
});