2014-06-10 14 views
2

我有這個代碼完美計數,但客戶希望它從16.5到9.99計數。我相信可以讓結尾的數字包含兩個十進制數字。作爲代碼底部的選項,您會注意到有一個十進制選項。也許在計數的一半,我可以將它從1位小數改爲2位小數?我怎樣才能做到這一點?謝謝!用小數點數jquery

(function($) { 
$.fn.countTo = function(options) { 
    // merge the default plugin settings with the custom options 
    options = $.extend({}, $.fn.countTo.defaults, options || {}); 

    // how many times to update the value, and how much to increment the value on each update 
    var loops = Math.ceil(options.speed/options.refreshInterval), 
     increment = (options.to - options.from)/loops; 

    return $(this).each(function() { 
     var _this = this, 
      loopCount = 0, 
      value = options.from, 
      interval = setInterval(updateTimer, options.refreshInterval); 

     function updateTimer() { 
      value += increment; 
      loopCount++; 
      $(_this).html(value.toFixed(options.decimals)); 

      if (typeof(options.onUpdate) == 'function') { 
       options.onUpdate.call(_this, value); 
      } 

      if (loopCount >= loops) { 
       clearInterval(interval); 
       value = options.to; 

       if (typeof(options.onComplete) == 'function') { 
        options.onComplete.call(_this, value); 
       } 
      } 
     } 
    }); 
}; 

$.fn.countTo.defaults = { 
    from: 0, // the number the element should start at 
    to: 100, // the number the element should end at 
    speed: 1000, // how long it should take to count between the target numbers 
    refreshInterval: 100, // how often the element should be updated 
    decimals: 1, // the number of decimal places to show 
    onUpdate: null, // callback method for every time the element is updated, 
    onComplete: null, // callback method for when the element finishes updating 
}; 
})(jQuery); 


jQuery(function($) { 
    $('#heroCounter').countTo({ 
     from: 16.5, 
     to: 9.9, 
     speed: 1500, 
     refreshInterval: 50, 
     onComplete: function(value) { 
      console.debug(this); 
     } 
    }); 
}); 

回答

0

是否要動態更改選項?如果是http://jsfiddle.net/tarabyte/82qBL/3/

添加選項支持的get/set

$.fn.countTo = function(options) { 
    if(typeof options === 'string') { 
     var name = arguments[0], 
      value = arguments[1]; 


     if(typeof value === 'undefined') { //getter 
      var data = this.eq(0).data('countTo'); 
      return data && data[name]; 
     } 
     return this.each(function(){ //setter 
      var data = $(this).data('countTo'); 
      data && (data[name] = value); 
     }); 
    } 

    // merge the default plugin settings with the custom options 
    options = $.extend({}, $.fn.countTo.defaults, options || {}); 
    ... 
    $(this).data('countTo', options); //store current options in element's data 

更改選項的onupdate

$('#heroCounter').countTo({ 
    from: 16.5, 
    to: 7.11, 
    speed: 2900, 
    decimals: 1, 
    refreshInterval: 50, 
    onUpdate: function(value){ 
     console.log($(this).countTo('from')); //get prop 
     if(value < 10) { 
      $(this).countTo('decimals', 2); //set prop  
     } 
    }, 
    onComplete: function(value) { 
     console.debug(this); 
    } 
}); 
+0

這工作完美。非常感謝您的幫助 – jdoimeadios23

+0

歡迎您:) –

1

如果我理解你應該增加decimals選項:

$('#heroCounter').countTo({ 
     from: 16.5, 
     to: 9.9, 
     speed: 1500, 
     decimals: 2, 
     refreshInterval: 50, 
     onComplete: function(value) { 
      console.debug(this); 
     } 
    }); 

或更改$(_this).html(value.toFixed(options.decimals));$(_this).html(value.toFixed(2));

JSFIDDLE

UPDATE: @Guillermo古鐵雷斯 - $(_this).html(value.toFixed(options.decimals, 2).toString().replace(/(\.[0-9]*?)0+$/, "$1"));

JSFIDDLE

+0

客戶希望它來計算從16.5到9.99那裏的第一個數字它應該顯示1小數點後面的數字最後以2位小數結束。在你提供的這個例子中,它顯示了兩位小數的數字。 – jdoimeadios23

+1

檢查此小提琴:http://jsfiddle.net/B2cEQ/1/只需修改第19行@ user3388636 fiddle:'$(_ this).html(value.toFixed(options.decimals,2).toString()。replace (/([.[0-9]*?)0+$/,「$ 1」));'This SO post help:http://stackoverflow.com/questions/1015402/chop-unused-decimals-with- javascript –