-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
71 lines (60 loc) · 1.93 KB
/
main.js
File metadata and controls
71 lines (60 loc) · 1.93 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
function animateCounter(element, endValue) {
const duration = 1000;
const steps = 30;
const stepValue = endValue / steps;
let currentStep = 0;
const timer = setInterval(() => {
currentStep++;
const currentValue = Math.min(stepValue * currentStep, endValue);
element.textContent = Math.floor(currentValue).toLocaleString();
if (currentStep >= steps) {
clearInterval(timer);
element.textContent = endValue.toLocaleString();
}
}, duration / steps);
}
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
if (entry.target.classList.contains('stat-number')) {
const target = parseInt(entry.target.dataset.target);
animateCounter(entry.target, target);
observer.unobserve(entry.target);
}
}
});
}, { threshold: 0.5 });
document.querySelectorAll('.stat-number').forEach(stat => {
observer.observe(stat);
});
let currentLang = 'en';
function setLanguage(lang) {
currentLang = lang;
document.documentElement.lang = lang;
document.querySelector('.current-lang').textContent = lang.toUpperCase();
updateTranslations();
localStorage.setItem('preferred-language', lang);
}
function updateTranslations() {
document.querySelectorAll('[data-translate]').forEach(element => {
const keys = element.dataset.translate.split('.');
let value = translations[currentLang];
for (const key of keys) {
value = value[key];
}
if (value) {
element.textContent = value;
}
});
}
document.addEventListener('DOMContentLoaded', () => {
const savedLang = localStorage.getItem('preferred-language');
const browserLang = navigator.language.split('-')[0];
const initialLang = savedLang || (translations[browserLang] ? browserLang : 'en');
setLanguage(initialLang);
document.querySelectorAll('.lang-option').forEach(option => {
option.addEventListener('click', () => {
setLanguage(option.dataset.lang);
});
});
});