inorganik / countUp.js

Animates a numerical value by counting to it
https://inorganik.github.io/countUp.js
MIT License
8.03k stars 1.38k forks source link

Wrapping individual digits in span / div #51

Closed wrabit closed 10 years ago

wrabit commented 10 years ago

Is this possible?

inorganik commented 10 years ago

What exactly are you trying to achieve? There's probably a way to do it using countUp as it works now.

wrabit commented 10 years ago

Ok, I added to the printValue function to achieve, not ideal and probably not the best written code but I thought I would post it incase others wanted to add the same functionality:

// Print value to target
this.printValue = function(value) {
    var width = 3;
    var result = (value) ? self.formatNumber(value) : '--';

    // 1. make string
    result = result + ''; 

    // 2. add padding of four 0's
    result = result.length >= 4 ? result : new Array(4 - result.length + 1).join('0') + result;

    // 3. wrap digits with span tags
    result = '<span>' + result.split('').join('<\/span><span>') + '<\/span>';

    if (self.d.tagName == 'INPUT') {
        this.d.value = result;
    } else {
        this.d.innerHTML = result;
    }
}
inorganik commented 10 years ago

Why do you need to put each digit in a span?

wrabit commented 10 years ago

As the title says... "Wrapping individual digits in span or div tags" I am adding styling to the counter where I need fixed dimensions for each digit.

inorganik commented 10 years ago

Oh - You need fixed dimensions for each digit - gotcha. You've got a good solution there.

mhbapcc commented 5 years ago

Is this method still working?

Ok, I added to the printValue function to achieve, not ideal and probably not the best written code but I thought I would post it incase others wanted to add the same functionality:

// Print value to target
this.printValue = function(value) {
    var width = 3;
    var result = (value) ? self.formatNumber(value) : '--';

    // 1. make string
    result = result + ''; 

    // 2. add padding of four 0's
    result = result.length >= 4 ? result : new Array(4 - result.length + 1).join('0') + result;

    // 3. wrap digits with span tags
    result = '<span>' + result.split('').join('<\/span><span>') + '<\/span>';

    if (self.d.tagName == 'INPUT') {
        this.d.value = result;
    } else {
        this.d.innerHTML = result;
    }
}