首页 > 解决方案 > jQuery 动画计数器 - 十进制或整数

问题描述

我正在尝试编辑以下脚本以显示带有小数或整数的数字,具体取决于属性值。目前,它以小数显示所有数字,因此 1.2 可以,但 90.0 不行。

jQuery('.counter-wrap .count').each(function () {
    var percent_hours = jQuery(this).attr('data-count');

    jQuery(this).prop('Counter', 0).animate({
            Counter: percent_hours
        }, {
        duration: 1000,
        easing: 'swing',
        step: function (now) {                      
            jQuery(this).text(this.Counter.toFixed(1));
        }
    });
});

jsfiddle

一些帮助将不胜感激。

标签: javascriptjquery

解决方案


您可以检查数字本身是否为小数。如果是十进制,您可以添加.toFixed函数

if(Math.round(decimalOrNot) === decimalOrNot) {
    //decimalOrNot is not a decimal
}
else {
    //decimalOrNot is decimal
}

推荐阅读