JSFiddle - React, Tailwind, and code Playground
HTML
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
<span class="Count">6666</span>
<br/>
<span class="Count">66666666</span>
<br/>
<span class="Count">66666666</span>
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
JavaScript
(function( $ ){
$(window).bind('scroll', function() {
var wintop = $(window).scrollTop();
var winheight = $(window).height();
$('.Count').each(function (i, counter) {
var $this = $(counter);
var eltop = $this.offset().top;
var elheight = $this.outerHeight(true);
if (eltop + elheight < wintop || eltop > wintop + winheight) {
if (!$this.hasClass('running')) {
$this.removeClass('started');
}
}
if ( (wintop + winheight) > eltop && !$this.hasClass('started')) {
$this.addClass('started').addClass('running');
jQuery({ Counter: 0 }).animate({ Counter: $this.text() }, {
duration: 2000,
easing: 'swing',
step: function (i) {
$this.text(Math.ceil(i));
},
complete : function() {
$this.removeClass('running');
},
});
}
});
});
} )(jQuery);