带符号的jQuery数字计数器
如何用符号从0数到数字?数字来自DB,使用自定义函数添加K、M、B,使用jquery计数器向上显示从0到1500的动画,但实际显示为1.5K 当我运行counter()时,它将K、M、B和1.5K移到1.50。我正试图获得精确的1.5公里,但与反风格的动画带符号的jQuery数字计数器,jquery,Jquery,如何用符号从0数到数字?数字来自DB,使用自定义函数添加K、M、B,使用jquery计数器向上显示从0到1500的动画,但实际显示为1.5K 当我运行counter()时,它将K、M、B和1.5K移到1.50。我正试图获得精确的1.5公里,但与反风格的动画 function number_format_short( $n, $precision = 1 ) { if ($n < 900) { // 0 - 900 $n_format = number
function number_format_short( $n, $precision = 1 ) {
if ($n < 900) {
// 0 - 900
$n_format = number_format($n, $precision);
$suffix = '';
} else if ($n < 900000) {
// 0.9k-850k
$n_format = number_format($n / 1000, $precision);
$suffix = 'K';
} else if ($n < 900000000) {
// 0.9m-850m
$n_format = number_format($n / 1000000, $precision);
$suffix = 'M';
} else if ($n < 900000000000) {
// 0.9b-850b
$n_format = number_format($n / 1000000000, $precision);
$suffix = 'B';
} else {
// 0.9t+
$n_format = number_format($n / 1000000000000, $precision);
$suffix = 'T';
}
// Remove unecessary zeroes after decimal. "1.0" -> "1"; "1.00" -> "1"
// Intentionally does not affect partials, eg "1.50" -> "1.50"
if ( $precision > 0 ) {
$dotzero = '.' . str_repeat( '0', $precision );
$n_format = str_replace( $dotzero, '', $n_format );
}
return $n_format . $suffix;
}
function counter(){
$('.cnts').each(function (index) {
var size = $(this).text().split(".")[1] ? $(this).text().split(".")[1].length : 0;
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 1500,
easing: 'swing',
step: function (now) {
$(this).text(parseFloat(now).toFixed(size));
}
});
});
}
function number\u format\u short($n,$precision=1){
如果($n<900){
// 0 - 900
$n_格式=数字_格式($n,$precision);
$suffix='';
}否则,如果($n<900000){
//0.9k-850k
$n_格式=数字_格式($n/1000,$precision);
$suffix='K';
}否则,如果($n<900000000){
//0.9m-850m
$n_格式=数字_格式($n/1000000,$precision);
$suffix='M';
}其他如果($n<90000000000){
//0.9b-850b
$n_格式=数字_格式($n/100000000,$precision);
$后缀='B';
}否则{
//0.9吨+
$n_格式=数字_格式($n/10000000000,$precision);
$suffix='T';
}
//删除小数点后不必要的零。“1.0”->“1”;“1.00”->“1”
//故意不影响偏音,例如“1.50”->“1.50”
如果($precision>0){
$dotzero='..str_repeat('0',$precision);
$n_format=str_replace($dotzero,,$n_format);
}
返回$n_格式。$后缀;
}
函数计数器(){
$('.cnts')。每个(函数(索引){
变量大小=$(this.text().split(“.”[1]?$(this.text().split(.”[1])。长度:0;
$(this.prop('Counter',0)。设置动画({
计数器:$(this.text())
}, {
持续时间:1500,
放松:"摇摆",,
步骤:功能(现在){
$(this).text(parseFloat(now).toFixed(size));
}
});
});
}
您可以这样做:
首先删除后缀字母(K、M、B等)并存储在变量上。并在更新文本时附加它
$('.cnts')。每个(函数(索引){
var letter=$(this.text().match(/\D$/)[0];/*将字母存储在变量上*/
var text=$(this).text().replace(/\D$/,“”);/*从字符串中删除字母,以便正确计算十进制数*/
变量大小=text.split(“.”[1]?text.split(“.”[1]。长度:0;
$(this.prop('Counter',0)。设置动画({
计数器:文本
}, {
持续时间:1500,
放松:"摇摆",,
步骤:功能(现在){
$(this).text(parseFloat(now).toFixed(size)+字母);/*在此处附加字母*/
}
});
});代码>
1.5K
您可以这样做:
首先删除后缀字母(K、M、B等)并存储在变量上。并在更新文本时附加它
$('.cnts')。每个(函数(索引){
var letter=$(this.text().match(/\D$/)[0];/*将字母存储在变量上*/
var text=$(this).text().replace(/\D$/,“”);/*从字符串中删除字母,以便正确计算十进制数*/
变量大小=text.split(“.”[1]?text.split(“.”[1]。长度:0;
$(this.prop('Counter',0)。设置动画({
计数器:文本
}, {
持续时间:1500,
放松:"摇摆",,
步骤:功能(现在){
$(this).text(parseFloat(now).toFixed(size)+字母);/*在此处附加字母*/
}
});
});代码>
1.5K
和php
在这里做什么?将K、M、B添加到数字中并进行四舍五入。无法复制您的问题。您是否在数据库中以1k(短数字格式)存储数据?请存储实际数据,而不是缩短它们@Zackin DB is eg 1496,它的四舍五入为1500,显示为1.5k,php
这里做什么?将K、M、B添加到数字并四舍五入。无法复制您的问题。您是否在数据库中以1k(短数字格式)存储数据?请存储实际数据,而不是缩短数据@Zackin DB is eg 1496,它的最大值为1500,显示为1.5k,而不是片段。您应该进行正则表达式匹配并替换/\D$/
,以避免修剪未固定值的最后一位。@apokryfos感谢您的建议。更新:)您应该进行正则表达式匹配并替换/\D$/
,而不是切片,以避免修剪未固定值的最后一位。@apokryfos感谢您的建议。更新:)