antonenkoab,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
.number{
float: left;
width: 100px;
font-size: 22px;
line-height: 44px;
color: #FFFFFF;
background-color: #8B4513;
text-align: center;
margin-right: 30px;
}
p{
height: 2000px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(function() {
var num = $(".number");
num.each(function(indx, el) {
var max = $(el).data("max");
var duration = 5000;
var visibility = checkViewport(el);
$(el).on("animeNum", function() {
$({n: 0}).animate({n: max}, {
easing: "linear",
duration: duration,
step: function(now, fx) {
$(el).html(now | 0)
}
})
}).data("visibility", visibility);
visibility && $(el).trigger("animeNum")
});
function checkViewport(el) {
var H = document.documentElement.clientHeight,
h = el.scrollHeight,
pos = el.getBoundingClientRect();
return pos.top + h > 0 && pos.bottom - h < H
}
$(window).scroll(function() {
num.each(function(indx, el) {
var visibility = checkViewport(el);
el = $(el);
var old = el.data("visibility");
old != visibility && el.data("visibility", visibility) && !old && el.trigger("animeNum")
})
})
});
</script>
</head>
<body>
<p></p>
<div class="number" data-max="3">0</div>
<div class="number" data-max="40">0</div>
<div class="number" data-max="400">0</div>
<p></p>
</body>
</html>