如何使用data属性创建一个jquery计数器,该属性检查值是整数还是十进制数?
这是您可以做的。。
HTML
<div><span class="count" data-number="50.50">0</span></div> <div><span class="count" data-number="100">0</span></div> <div><span class="count" data-number="10">0</span></div>
jQuery
jQuery(function($) {
$('.count').each(function () {
var $numberValue = $(this).data('number');
// If Number is a whole number do this..
if($numberValue == Math.floor($numberValue)) {
$(this).prop('Counter', 0).animate({
Counter: $(this).data('number')
}, {
duration: 1000,
easing: 'swing',
step: function(now) {
$(this).text(Math.ceil(now).toLocaleString('en'));
}
});
}
// If Number is a decimal number do this..
else {
$(this).prop('Counter', 0).animate({
Counter: $(this).data('number')
}, {
duration: 1000,
easing: 'swing',
step: function(now) {
$(this).text(this.Counter.toFixed(1));
}
});
}
});
});