-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStats Module.code.javascript
More file actions
53 lines (50 loc) · 1.72 KB
/
Stats Module.code.javascript
File metadata and controls
53 lines (50 loc) · 1.72 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
// js/modules/stats.js
export const initStats = () => {
const animateValue = (obj, start, end, duration) => {
let startTimestamp = null;
const step = (timestamp) => {
if (!startTimestamp) startTimestamp = timestamp;
const progress = Math.min((timestamp - startTimestamp) / duration, 1);
obj.innerHTML = Math.floor(progress * (end - start) + start)
.toLocaleString('en-US', {
maximumFractionDigits: 2
});
if (progress < 1) {
window.requestAnimationFrame(step);
}
};
window.requestAnimationFrame(step);
};
// Initialize stats when they come into view
const statsObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const statValue = entry.target;
const finalValue = parseFloat(statValue.getAttribute('data-value'));
animateValue(statValue, 0, finalValue, 2000);
statsObserver.unobserve(statValue);
}
});
}, {
threshold: 0.5
});
document.querySelectorAll('.stat-value').forEach(stat => {
statsObserver.observe(stat);
});
};
export const updateLiveStats = () => {
// Simulated live price updates
setInterval(() => {
const priceElement = document.querySelector('.live-price');
if (priceElement) {
const currentPrice = parseFloat(priceElement.getAttribute('data-price'));
const change = (Math.random() - 0.5) * 0.01;
const newPrice = currentPrice * (1 + change);
priceElement.textContent = `$${newPrice.toFixed(4)}`;
priceElement.classList.add(change > 0 ? 'price-up' : 'price-down');
setTimeout(() => {
priceElement.classList.remove('price-up', 'price-down');
}, 1000);
}
}, 5000);
};