2017-10-10 33 views
0

我用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>

+0

你最好讓你的代碼乾淨第一,然後纔拿出來給我們。這是幾乎不可能讀...閱讀關於吻)) –

+0

什麼是難以閱讀,例如? – bars96

+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,這是二十五分之一百三十零= 5.2。然後,t是實際上將l - n = 130 - 5.2 = 124.8,這就是爲什麼它數到125,而不是30

一個簡單的解決方案將是添加data-increment="1"h1標籤。您可以考慮更改的默認計算10變量也是。

編輯:

我看到你編輯你的代碼,這是偉大的,因爲它更易於調試。我認爲解決方案應該改變這條線:(start = numb, !0);爲這樣的:self.find(".countup-amount").html(numb);它基本上意味着如果當前數字+增量值大於目標值,這意味着它是最後一步,我們可以簡單地填充我們的目標值爲span

+0

非常感謝 – bars96

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);