我用rewrited hsCountdown庫返回錯誤的值和一些數字就顯示錯誤的價值觀:我上面規定的「130」爲增量
在示例,但hsCountdown
它僅增大至125 爲什麼?jQuery的hsCountdown的一些數字
我被調試爲「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;
\t \t \t \t winTop <= self.offset().top && winTop + winHeight >= self.offset().top && (timer = setTimeout(function u() {
\t \t \t \t \t var currentIncrement = (fractional > 0 ? start.toFixed(fractional) : start);
\t \t \t \t \t $('#debug_console').append('[Condition debug] (currentIncrement <= realMaxNumber) equals ('+currentIncrement+' <= '+realMaxNumber+')<br>');
\t \t \t \t \t return (currentIncrement <= realMaxNumber)
\t \t \t \t \t
\t \t \t \t \t ?
\t \t \t \t \t
\t \t \t \t \t (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)))
\t \t \t \t \t
\t \t \t \t \t :
\t \t \t \t \t
\t \t \t \t \t (start = numb, !0);
\t \t \t \t }, 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>
你最好讓你的代碼乾淨第一,然後纔拿出來給我們。這是幾乎不可能讀...閱讀關於吻)) –
什麼是難以閱讀,例如? – bars96
例如,變量名稱。對不起,如果我傷害你的感受。我不打算這樣做 –