| 1 |
(function ($) { |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
function eelRunCounter($scope) { |
| 5 |
const counters = $scope.find(".eel-counter"); |
| 6 |
if (!counters.length) return; |
| 7 |
|
| 8 |
const observer = new IntersectionObserver(entries => { |
| 9 |
entries.forEach(entry => { |
| 10 |
if (!entry.isIntersecting) return; |
| 11 |
|
| 12 |
const counter = entry.target; |
| 13 |
observer.unobserve(counter); |
| 14 |
|
| 15 |
const target = parseInt(counter.dataset.count, 10) || 0; |
| 16 |
const duration = parseInt(counter.dataset.duration, 10) || 1000; |
| 17 |
const format = counter.dataset.format || "default"; |
| 18 |
|
| 19 |
let current = 0; |
| 20 |
const startTime = performance.now(); |
| 21 |
|
| 22 |
function formatNumber(num) { |
| 23 |
if (num < 1000) return num; |
| 24 |
|
| 25 |
const groups = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, { |
| 26 |
comma: ",", |
| 27 |
dot: ".", |
| 28 |
space: " ", |
| 29 |
underline: "_" |
| 30 |
}[format] || ""); |
| 31 |
|
| 32 |
return format === "underline" ? `${groups}` : groups; |
| 33 |
} |
| 34 |
|
| 35 |
function animate(time) { |
| 36 |
const progress = Math.min((time - startTime) / duration, 1); |
| 37 |
current = Math.floor(progress * target); |
| 38 |
|
| 39 |
if (format === "underline") { |
| 40 |
counter.innerHTML = formatNumber(current); |
| 41 |
} else { |
| 42 |
counter.textContent = formatNumber(current); |
| 43 |
} |
| 44 |
|
| 45 |
if (progress < 1) requestAnimationFrame(animate); |
| 46 |
} |
| 47 |
|
| 48 |
requestAnimationFrame(animate); |
| 49 |
}); |
| 50 |
}, { threshold: 0.2 }); |
| 51 |
|
| 52 |
counters.each(function () { |
| 53 |
observer.observe(this); |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
$(window).on("elementor/frontend/init", function () { |
| 58 |
elementorFrontend.hooks.addAction("frontend/element_ready/widget", function ($scope) { |
| 59 |
if ($scope.find(".eel-counter").length) { |
| 60 |
eelRunCounter($scope); |
| 61 |
} |
| 62 |
}); |
| 63 |
}); |
| 64 |
|
| 65 |
})(jQuery); |
| 66 |
|