Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向动画计数函数添加逗号_Javascript_Jquery_Animation_Comma - Fatal编程技术网

Javascript 向动画计数函数添加逗号

Javascript 向动画计数函数添加逗号,javascript,jquery,animation,comma,Javascript,Jquery,Animation,Comma,我已经尝试了所有我能在网上找到的关于这个的东西,但它们似乎都是处理静态数字,而不是动画数字 通过修改此函数,可以在数字增加时将逗号添加到我的值中吗 $('.counter').each(function() { var $this = $(this), countTo = $this.attr('data-count'); $({ countNum: $this.text()}).animate({ countNu

我已经尝试了所有我能在网上找到的关于这个的东西,但它们似乎都是处理静态数字,而不是动画数字

通过修改此函数,可以在数字增加时将逗号添加到我的值中吗

$('.counter').each(function() {
        var $this = $(this),
            countTo = $this.attr('data-count');

        $({ countNum: $this.text()}).animate({
            countNum: countTo
        },

        {

        duration: 500,
        easing:'linear',
        step: function() {
            $this.text(Math.floor(this.countNum));
        },
        complete: function() {
            $this.text(this.countNum);
              //alert('finished');
        }

    }); 

只要在当前设置元素
text()
的任何位置添加逗号即可

逗号代码从

$('.counter')。每个(函数(){
变量$this=$(this),
countTo=$this.attr('data-count');
$({
countNum:$this.text()
}).制作动画({
countNum:countTo
},
{
持续时间:5000,
“线性”,
步骤:函数(){
$this.text(commaSeparateNumber(Math.floor(this.countNum));
},
完成:函数(){
$this.text(commaSeparateNumber(this.countNum));
//警报(“完成”);
}
}
);
});
函数commaSeparateNumber(val){
while(/(\d+)(\d{3})/.test(val.toString()){
val=val.toString();
}
返回val;
}

如果字符串包含逗号,请使用
parseFloat
。 请检查以下工作代码

$('.counter').each(function() {
    $(this).prop('Counter', 0).animate({
        Counter: parseFloat($(this).text().replace(/,/g, ''))
    }, {
        duration: 10000,
        easing: 'swing',
        step: function(now) {
            $(this).text(getRupeesFormat(Math.ceil(now)));
        }
    });
});


function getRupeesFormat(val) {
    while (/(\d+)(\d{3})/.test(val.toString())) {
        val = val.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
    }
    return val;
}

你们今天想成为有趣的人和语法纳粹分子。你会读javascript吗?如果您不理解此脚本将计数从0动画化为html数据属性中定义的数字,那么您可能无法回答我的问题。@JSum,编辑其他用户的语法和清晰度问题是一个行之有效的工作方式。对这个问题嗤之以鼻是一个很好的办法,可以让你的问题得不到回答(如果我在回答之前看到你的评论,我就不会花时间来帮助你了。)我在这里问了很多问题,这是我第一次因为偶然的语法错误而被否决。你回答了,这对我来说已经足够好了。我在这里回答了很多问题,这是我第一次让提问者如此粗鲁。这段代码比许多github反项目工作得更好,而且非常轻量级。谢谢