|
| 1 | +import { CountUp } from 'countup.js'; |
| 2 | + |
| 3 | +const countUpOptions = { |
| 4 | + separator: ' ', |
| 5 | + decimal: ',', |
| 6 | + suffix: ' €', |
| 7 | + duration: 1.5, |
| 8 | +}; |
| 9 | + |
| 10 | +let amountCountUp = null; |
| 11 | +let lastDonorCount = null; |
| 12 | + |
| 13 | +function initCountUp() { |
| 14 | + if (!amountCountUp) { |
| 15 | + const amountEl = document.getElementById('card-amount'); |
| 16 | + if (amountEl && typeof CountUp !== 'undefined') { |
| 17 | + const startVal = window.currentAmount / 100; |
| 18 | + amountCountUp = new CountUp('card-amount', startVal, countUpOptions); |
| 19 | + if (!amountCountUp.error) { |
| 20 | + amountCountUp.start(); |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
1 | 26 | function updateCardWidget() { |
2 | 27 | const currentAmountUnit = window.currentAmount / 100; |
3 | 28 | const goal = window.goalAmount || 1; |
4 | 29 | const percentage = Math.min(100, Math.round((currentAmountUnit / goal) * 100)); |
5 | 30 |
|
6 | | - const amountEl = document.getElementById('card-amount'); |
7 | 31 | const barFill = document.getElementById('card-bar-fill'); |
8 | 32 | const percentEl = document.getElementById('card-percentage'); |
9 | 33 | const donorsEl = document.getElementById('card-donors'); |
10 | 34 |
|
11 | | - if (amountEl) amountEl.textContent = currentAmountUnit.toLocaleString('fr-FR') + ' €'; |
| 35 | + // Animate amount with CountUp |
| 36 | + initCountUp(); |
| 37 | + if (amountCountUp) { |
| 38 | + amountCountUp.update(currentAmountUnit); |
| 39 | + } else { |
| 40 | + const amountEl = document.getElementById('card-amount'); |
| 41 | + if (amountEl) amountEl.textContent = currentAmountUnit.toLocaleString('fr-FR') + ' €'; |
| 42 | + } |
| 43 | + |
12 | 44 | if (barFill) barFill.style.width = percentage + '%'; |
13 | 45 | if (percentEl) percentEl.textContent = percentage + '%'; |
14 | | - if (donorsEl) donorsEl.textContent = window.donorCount ?? 0; |
| 46 | + |
| 47 | + // Animate donor count change |
| 48 | + const newDonorCount = window.donorCount ?? 0; |
| 49 | + if (donorsEl) { |
| 50 | + if (lastDonorCount !== null && newDonorCount !== lastDonorCount) { |
| 51 | + donorsEl.classList.add('card-widget__pulse'); |
| 52 | + setTimeout(() => donorsEl.classList.remove('card-widget__pulse'), 600); |
| 53 | + } |
| 54 | + donorsEl.textContent = newDonorCount; |
| 55 | + } |
| 56 | + lastDonorCount = newDonorCount; |
15 | 57 | } |
16 | 58 |
|
17 | 59 | export default updateCardWidget; |
|
0 commit comments