Spaces:
Running
Running
File size: 5,671 Bytes
e58c862 | 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 | // Initialize app
document.addEventListener('DOMContentLoaded', function() {
initializeNavigation();
initializeProgressBars();
initializeScrollEffects();
initializeLinks();
});
// Navigation functionality
function initializeNavigation() {
const navLinks = document.querySelectorAll('.nav-link');
const sections = document.querySelectorAll('section[id]');
// Handle navigation active states
function updateActiveNav() {
let current = = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (window.scrollY >= sectionTop - 100) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${current}`) {
link.classList.add('active');
}
});
}
window.addEventListener('scroll', updateActiveNav);
// Smooth scroll for navigation links
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetSection = document.getElementById(targetId);
if (targetSection) {
targetSection.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
}
// Progress bar animations
function initializeProgressBars() {
const progressBars = document.querySelectorAll('.bg-slate-200 > div');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const bar = entry.target;
const width = bar.style.width;
bar.style.width = '0%';
setTimeout(() => {
bar.style.transition = 'width 1s ease-out';
bar.style.width = width;
}, 100);
}
});
});
progressBars.forEach(bar => {
observer.observe(bar);
});
}
// Scroll effects
function initializeScrollEffects() {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('fade-in');
}
});
}, observerOptions);
const elements = document.querySelectorAll('.bg-white');
elements.forEach(el => observer.observe(el));
}
// Interactive elements
function initializeLinks() {
// Quick action buttons
const quickActions = document.querySelectorAll('.bg-white.rounded-xl.p-4');
quickActions.forEach(button => {
button.addEventListener('click', function() {
const action = this.querySelector('span').textContent;
handleQuickAction(action);
});
});
// AI coach button
const aiButton = document.querySelector('.bg-thai-600');
if (aiButton) {
aiButton.addEventListener('click', function() {
showDetailedRecommendations();
});
}
}
// Handle quick actions
function handleQuickAction(action) {
switch(action) {
case 'ถ่ายอาหาร':
openFoodCamera();
break;
case 'เริ่มออกกำลัง':
startWorkout();
break;
case 'เริ่ม IF':
startIntermittentFasting();
break;
case 'บันทึกอารมณ์':
openMoodTracker();
break;
}
}
// API simulation functions
function openFoodCamera() {
// Simulate opening camera for food logging
alert('เปิดกล้องเพื่อถ่ายอาหาร...');
}
function startWorkout() {
// Simulate starting workout tracking
alert('เริ่มติดตามการออกกำลังกาย...');
}
function startIntermittentFasting() {
// Simulate starting IF timer
alert('เริ่มจับเวลาการอดอาหาร...');
}
function openMoodTracker() {
// Simulate opening mood tracker
alert('เปิดตัวติดตามอารมณ์...');
}
function showDetailedRecommendations() {
// Simulate showing detailed AI recommendations
alert('กำลังโหลดคำแนะนำเพิ่มเติม...');
}
// Real-time updates simulation
function simulateRealTimeUpdates() {
const recoveryScore = document.querySelector('.text-green-600');
const strainScore = document.querySelector('.text-orange-600');
setInterval(() => {
// Simulate slight variations in scores
if (recoveryScore && strainScore) {
const newRecovery = Math.floor(Math.random() * 5) + 75;
const newStrain = (Math.random() * 2 + 11).toFixed(1);
recoveryScore.textContent = newRecovery + '%';
strainScore.textContent = newStrain;
}
}, 30000); // Update every 30 seconds
}
// Initialize real-time updates
simulateRealTimeUpdates();
// Handle page visibility changes
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'visible') {
// Refresh data when user returns to page
location.reload();
}
}); |