javascript - 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));
}
});
});
一些帮助将不胜感激。
解决方案
您可以检查数字本身是否为小数。如果是十进制,您可以添加.toFixed函数
if(Math.round(decimalOrNot) === decimalOrNot) {
//decimalOrNot is not a decimal
}
else {
//decimalOrNot is decimal
}
推荐阅读
- android - Android后退按钮应用于所有屏幕反应原生错误
- javascript - 文本垂直出现在反应引导程序中
- javascript - prev() 方法不适用于前一个元素
- java - 实体更新后休眠搜索6更新弹性搜索索引
- python - 我在我的代码中找不到错误,以及为什么我没有成为 a 和 b 的答案
- karate - 空手道 DSL - 使用重试直到检查数组,直到所有项目中都存在特定的键值对
- node.js - loopback 4 使用中间件 prometheus-api-metrics
- c - doxygen compact #define 文档
- javascript - 将对象数组合并为单个对象
- flutter - 在 Flutter 的许多不同类中使用变量的最佳实践是什么?