Это простейший плагин счетчика jQuery, который подсчитывает или увеличивает счет до указанного числа с настраиваемым замедлением и поддержкой продолжительности.
HTML
Код
<div class="row">
<h1>Growth By Numbers</h1>
<p class="para">We have grown from strength to strength over the past 20 years</p>
<div class="col-md-2 col-md-offset-2"> <h1><span class="counter">20,000</span></h1>
<p>DELIVERIES IN 2015</p>
</div>
<div class="col-md-2">
<h1><span class="counter">7,300</span></h1>
<p>DAYS OF OPERATION</p>
</div>
<div class="col-md-2">
<h1><span class="counter">2,500</span></h1>
<p>WORKERS CLOCKING IN</p>
</div>
<div class="col-md-2">
<h1><span class="counter">1,000</span></h1>
<p>SATISFIED CUSTOMERS</p>
</div>
</div>
CSS
Код
body{
background:#073763;
text-align:center;
color:#fff;
}
.row{
margin-top:70px;
}
.counter{
color: #f1c232;
}
div.col-md-2{
border-right:#ddd solid 0.1px;
}
div.col-md-2:last-child{
border-right:#ddd solid 0px;
}
JS
Код
(function( $ ){
"use strict";
$.fn.counterUp = function( options ) {
// Defaults
var settings = $.extend({
'time': 400,
'delay': 10
}, options);
return this.each(function(){
// Store the object
var $this = $(this);
var $settings = settings;
var counterUpper = function() {
var nums = [];
var divisions = $settings.time / $settings.delay;
var num = $this.text();
var isComma = /[0-9]+,[0-9]+/.test(num);
num = num.replace(/,/g, '');
var isInt = /^[0-9]+$/.test(num);
var isFloat = /^[0-9]+\.[0-9]+$/.test(num);
var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0;
// Generate list of incremental numbers to display
for (var i = divisions; i >= 1; i--) {
// Preserve as int if input was int
var newNum = parseInt(num / divisions * i);
// Preserve float if input was float
if (isFloat) {
newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
}
// Preserve commas if input had commas
if (isComma) {
while (/(\d+)(\d{3})/.test(newNum.toString())) {
newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
}
nums.unshift(newNum);
}
$this.data('counterup-nums', nums);
$this.text('0');
// Updates the number until we're done
var f = function() {
$this.text($this.data('counterup-nums').shift());
if ($this.data('counterup-nums').length) {
setTimeout($this.data('counterup-func'), $settings.delay);
} else {
delete $this.data('counterup-nums');
$this.data('counterup-nums', null);
$this.data('counterup-func', null);
}
};
$this.data('counterup-func', f);
// Start the count up
setTimeout($this.data('counterup-func'), $settings.delay);
};
// Perform counts when the element gets into view
$this.waypoint(counterUpper, { offset: '100%', triggerOnce: true });
});
};
})( jQuery );
// custom code
jQuery(document).ready(function( $ ) {
$('.counter').counterUp({
delay: 10,
time: 1000
});
});
Подсчет чисел с помощью CounterUP JS
See the Pen
Counting by Kocsten (@kocsten)
on CodePen.