ws_small_y-1.0.x-dev/modules/ws_small_y_statistics/assets/js/statistics-counter.js
modules/ws_small_y_statistics/assets/js/statistics-counter.js
/**
* Stats counter.
*/
(function ($, Drupal) {
// Sets height of the div based on its width.
let $statisticsContentItem = $('.statistics-content-item .inner');
function setStatItemHeight() {
$statisticsContentItem.each(function (){
$(this).css({height: $(this).outerHeight() + 100});
});
}
let $countElements = $('.js-count');
// Animate the statistic up with commas.
function animateNumber() {
$countElements.each(function () {
let $this = $(this),
countTo = $this.attr("data-count"),
countDuration = $this.attr("data-duration");
$({countNum: $this.text()}).animate(
{
countNum: countTo
},
{
duration: countDuration ? parseInt(countDuration) : 3000,
easing: "linear",
step: function () {
// Count up with commas
$this.text(Math.floor(this.countNum).toLocaleString("en"));
},
complete: function () {
// Add comma after done counting
$this.text(this.countNum.toLocaleString("en"));
}
}
);
});
}
// Set the counter back to 0.
function resetNumber() {
$countElements.each(function () {
$(this).text('0');
});
}
let controller = new ScrollMagic.Controller();
let $lbStatsBlock = $(".lb-stats-block");
// When scrolling in & out of view.
new ScrollMagic.Scene({
triggerElement: ".lb-stats-block"
})
.on("enter", function () {
$lbStatsBlock.addClass("lb-stats-block-animated");
animateNumber();
})
.on("leave", function () {
$lbStatsBlock.removeClass("lb-stats-block-animated");
resetNumber();
})
.addTo(controller);
$(window).resize(function (){
setStatItemHeight();
}).resize();
})(jQuery, Drupal);
