Last active
August 29, 2015 14:08
-
-
Save elin-moco/6eab253e0dc12e78560f to your computer and use it in GitHub Desktop.
Simple Integer Counter JQuery Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.countInt = function(duration, from) { | |
from = typeof from !== 'undefined' ? from : 0; | |
duration = typeof duration !== 'undefined' ? duration : 1000; | |
var $this = $(this); | |
var to = $this.text(); | |
$this.text(from); | |
$this.start = function() { | |
var stime = new Date().getTime(); | |
function step() { | |
var now = new Date().getTime(); | |
var progress = now - stime; | |
var oldValue = $this.text(); | |
var value = (to - from) * progress / duration; | |
if (Math.abs(value - oldValue) >= 1) { | |
$this.text(Math.floor(value)); | |
} | |
if (progress <= duration) { | |
requestAnimationFrame(step); | |
} | |
else { | |
requestAnimationFrame(function() { | |
$this.text(to); | |
}); | |
} | |
} | |
requestAnimationFrame(step); | |
return $this; | |
}; | |
return $this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment