2017-08-04 45 views
0

我在我的網站上有一個很酷的櫃檯。它基本上以一定的速率計數到一定數量。我希望它能夠達到7.2,但它只會達到7,可能是因爲它被「。」弄糊塗了。代碼如下。對於像 這樣的時間段,不確定任何奇特的代碼。謝謝!如何在<span>中放置.2?

<div class="col_one_fourth nobottommargin center col_last" data-animate="bounceIn" data-delay="600"> 
    <i class="i-plain i-xlarge divcenter nobottommargin icon-phone"></i> 
    <div class="counter counter-lined"><span data-from="60" data-to="7.2" data-refresh-interval="30" data-speed="2700"></span></div> 
    <h5>BILLION DEVICES! On Earth!</h5> 

Java腳本

counter: function(){ 

     if(!$().appear) { 
      console.log('counter: Appear not Defined.'); 
      return true; 
     } 

     if(!$().countTo) { 
      console.log('counter: countTo not Defined.'); 
      return true; 
     } 

     var $counterEl = $('.counter:not(.counter-instant)'); 
     if($counterEl.length > 0){ 
      $counterEl.each(function(){ 
       var element = $(this); 
       var counterElementComma = $(this).find('span').attr('data-comma'); 
       if(!counterElementComma) { counterElementComma = false; } else { counterElementComma = true; } 
       if($body.hasClass('device-lg') || $body.hasClass('device-md')){ 
        element.appear(function(){ 
         SEMICOLON.widget.runCounter(element, counterElementComma); 
         if(element.parents('.common-height')) { 
          SEMICOLON.initialize.maxHeight(); 
         } 
        },{accX: 0, accY: -120},'easeInCubic'); 
       } else { 
        SEMICOLON.widget.runCounter(element, counterElementComma); 
       } 
      }); 
     } 
    }, 

    runCounter: function(counterElement,counterElementComma){ 
     if(counterElementComma == true) { 
      counterElement.find('span').countTo({ 
       formatter: function (value, options) { 
        value = value.toFixed(options.decimals); 
        value = value.replace(/\B(?=(\d{3})+(?!\d))/g, ','); 
        return value; 
       } 
      }); 
     } else { 
      counterElement.find('span').countTo(); 
     } 
    }, 
+1

我相信會有一些JavaScript與此相伴隨。 – fubar

+0

它是否轉換爲'int'並且您需要將其指定爲'float'? –

+0

data-to應該處理得很好,其代碼中的其他內容會導致問題。再放一些代碼,以便我們檢查。 – Icewine

回答

1

你還在這裏缺少一些代碼,因爲有明確的功能被添加到的東西,你沒有展示。有沒有jquery插件或你已經安裝的東西?

你可能會找出你要找的東西,找出該插件如何配置它傳遞給格式化程序的選項。注:

formatter: function (value, options) { 
      value = value.toFixed(options.decimals); 

它的值設置爲基礎的options.decimals固定小數。如果要麼設置爲0,或者它根本沒有設置,你會看到你觀察到的行爲,它的設置0的小數位數,這截斷7.2至7

UPDATE

OK,如果你只是在你的評論中添加空格和換行符,它就變得非常清晰。有一條線設置選項對象,並從數據屬性中提取所有內容,例如data-decimals='2'將通過decimals:e(this).data("decimals")傳遞給params,並將小數位數設置爲2,您的7將爲7.20。

+0

這裏是我找到的「countto」插件的JS小提琴https://jsfiddle.net/as0qh76x/。還有一個「倒計時」 –

+0

不幸的是,這對我來說都是胡言亂語,但我注意到它說十進制很多次。 –

+0

看看那個。工作完美無瑕。非常感謝你 –

相關問題