我使用重写的hsCountdown库,对于某些数字,它显示错误的值:
在上面的示例中,我指定“130”表示增量,但hsCountdown
仅将其递增至125. 为什么?
我正在调试“r”变量(位于#debug_console
的位置)以及你的意思是什么?这个变量神奇地增加不是整数,而是浮点数
例如,54.0000000001而不是54. JavaScript,你喝醉了!
(function(a) {
"use strict";
a.fn.hsCounter = function(b) {
var c = a.extend({
delay: 50,
signPos: "after",
classVisible: "countup-vis",
decimalSeparator: ".",
orderSeparator: " "
}, b);
return this.each(function() {
b && a.extend(c, b);
var timer, num, line, content, self = a(this),
win = a(window),
winTop = win.scrollTop(),
winHeight = win.height(),
numb = self.data("num"),
increment = self.data("increment") ? self.data("increment") : (numb / 25 > 1.0 ? numb / 25 : 1.0),
fractional = self.data("fractional") ? self.data("fractional") : 0,
sign = self.data("sign") ? self.data("sign") : "",
regExp = /(\d)(?=(\d\d\d)+([^\d]|$))/g,
start = self.data("start") ? +self.data("start") : 0,
amount = a(".countup-amount"),
realMaxNumber = numb - increment;
winTop <= self.offset().top && winTop + winHeight >= self.offset().top && (timer = setTimeout(function u() {
var currentIncrement = (fractional > 0 ? start.toFixed(fractional) : start);
$('#debug_console').append('[Condition debug] (currentIncrement <= realMaxNumber) equals ('+currentIncrement+' <= '+realMaxNumber+')<br>');
return (currentIncrement <= realMaxNumber)
?
(start += increment, num = start.toFixed(fractional).replace(".", c.decimalSeparator).replace(regExp, "$1" + c.orderSeparator), content = self.find(amount).html(num), "after" == c.signPos ? self.html('<span class="countup-amount">' + num + '</span><span class="countup-sign">' + sign + "</span>") : "before" == c.signPos && self.html('<span class="countup-sign">' + sign + '</span><span class="countup-amount">' + num + "</span>"), self.hasClass("progress-up") && (self.html(self.html() + "<ins/>"), self.find("ins").css("width", start + "%")), self.parent().hasClass("countup-wrap") && (line = self.parent().find(".countup-line"), line.css("width", start + "%")), void(timer = setTimeout(u, c.delay)))
:
(start = numb, !0);
}, c.delay));
});
}
})(jQuery);
function initCounter(a, b) { b ? a.hsCounter() : a.text("0"); }
initCounter($(".counterup"), 1);
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<h1 class="counterup js" data-num="130">...</h1>
<div id="debug_console"></div>
答案 0 :(得分:1)
问题依赖于这些方面:
n = h.data("increment") ? h.data("increment") : (l / 25 > 1.0 ? l / 25 : 1.0),
和t = l - n;
t
基本上是此代码所依据的数字。 t
计算为l
(data-num
属性,在这种情况下为130减去n
,它是从data-increment
属性派生的。
如您所见,您未提供data-increment
,代码默认为l / 25
,即130/25 = 5.2。然后,t
实际上是l - n
= 130 - 5.2
= 124.8,这就是为什么它计为125而不是30。
一个简单的解决方案是将data-increment="1"
添加到您的h1
代码中。您也可以考虑更改n
变量的默认计算。
修改强>
我看到你编辑了代码,这很好,因为它更容易调试。我认为解决方案应该是改变这一行:(start = numb, !0);
如下:self.find(".countup-amount").html(numb);
它基本上意味着如果当前数字+增量值大于目标值,则意味着它是最后一个步骤,我们可以简单地使用目标值填充span
。
答案 1 :(得分:0)
修正了JS代码
(function(a) {
"use strict";
a.fn.hsCounter = function(b) {
var c = a.extend({
delay: 50,
signPos: "after",
classVisible: "countup-vis",
decimalSeparator: ".",
orderSeparator: " "
}, b);
return this.each(function() {
b && a.extend(c, b);
var timer, num, line, content, self = a(this),
win = a(window),
winTop = win.scrollTop(),
winHeight = win.height(),
numb = self.data("num"),
fractional = self.data("fractional") ? self.data("fractional") : 0,
decim = fractional > 0 ? 0.5 : 1.0,
increment = self.data("increment") ? self.data("increment") : (numb / 25 > decim ? numb / 25 : decim),
sign = self.data("sign") ? self.data("sign") : "",
regExp = /(\d)(?=(\d\d\d)+([^\d]|$))/g,
start = self.data("start") ? +self.data("start") : 0,
amount = a(".countup-amount");
winTop <= self.offset().top && winTop + winHeight >= self.offset().top && (timer = setTimeout(function u() {
if ((fractional > 0 ? start.toFixed(fractional) : start) < numb) {
var stin = (start + increment > numb) ? (start = numb) : (start += increment);
return (stin, num = start.toFixed(fractional).replace(".", c.decimalSeparator).replace(regExp, "$1" + c.orderSeparator), content = self.find(amount).html(num), "after" == c.signPos ? self.html('<span class="countup-amount">' + num + '</span><span class="countup-sign">' + sign + "</span>") : "before" == c.signPos && self.html('<span class="countup-sign">' + sign + '</span><span class="countup-amount">' + num + "</span>"), self.hasClass("progress-up") && (self.html(self.html() + "<ins/>"), self.find("ins").css("width", start + "%")), self.parent().hasClass("countup-wrap") && (line = self.parent().find(".countup-line"), line.css("width", start + "%")), void(timer = setTimeout(u, c.delay)));
} else {
return (start = numb, !0);
}
}, c.delay));
});
}
})(jQuery);
function initCounter(a, b) { b ? a.hsCounter() : a.text("0"); }
initCounter($(".counterup"), 1);