Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Created September 10, 2021 12:41
Show Gist options
  • Select an option

  • Save pavlo-bondarchuk/f56a72c39d87a073cdb20016af944385 to your computer and use it in GitHub Desktop.

Select an option

Save pavlo-bondarchuk/f56a72c39d87a073cdb20016af944385 to your computer and use it in GitHub Desktop.
count text
document.addEventListener('DOMContentLoaded', function(event) {
var $ = jQuery;
if (!$('body').hasClass('wp-admin')) {
$(window).on('loads scroll', function() {
var element = $('.counter');
var top_of_element = element.offset().top;
var bottom_of_element = element.offset().top + element.outerHeight();
var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();
var top_of_screen = $(window).scrollTop();
element.each(function() {
if ((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element) && !$(this).hasClass('animated')){
var $this = $(this),
countTo = $this.attr('data-count');
$this.addClass('animated');
$({ countNum: $this.text()}).animate({
countNum: countTo
},
{
duration: 2500,
easing:'linear',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}
});
}
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment