Skip to content

Instantly share code, notes, and snippets.

@Loiree
Created April 1, 2017 13:12
Show Gist options
  • Save Loiree/39b5169d097f57a92e2a829181df192c to your computer and use it in GitHub Desktop.
Save Loiree/39b5169d097f57a92e2a829181df192c to your computer and use it in GitHub Desktop.
Counter — увеличивает число в выбранном объекте от заданного до нужного с анимацией
<button onclick="ev.emit('counter:set', {obj: document.getElementsByClassName('counter')[0], countCur: 400, countNext: 700} )">Увеличить с 400 до 700</button>
<button onclick="ev.emit('counter:set', {obj: document.getElementsByClassName('counter')[0], countCur: 700, countNext: 400} )">Уменьшить с 700 до 400</button>
<div class="counter">400</div>
// Увеличивает число в выбранном объекте от заданного до нужного с анимацией
// obj {
// obj: объект, в котором находится число,
// countCur: текущее значение,
// countNext: нужное значение
// }
var Counter = (function() {
return {
init: function() {
this.sub();
},
sub: function() {
var self = this;
ev.on("counter:set", function(obj) {
self.set(obj);
});
},
set: function(obj) {
var fn = function(progress) {
obj.obj.innerHTML = Math.floor(obj.countCur + progress * (obj.countNext - obj.countCur) );
}
AnimHandler.init(fn, 400, "linear");
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment