I'm wanting to get that animation effect when numbers change quickly, example: http://jsbin.com/kevalutupe/1/
I'm wanting to do this CSS alone (I know how to code it in JS), I don't want to use JS as I feel hammering the DOM isn't the best solution. Is this at all possible with CSS?
I'm not fussed about the numbers actually incrementing correctly, I'm just after the effect.
The number spinning effect is most definitely possible using CSS3 animations and what's better you can also set the end-point using a small bit of JS and actually get the whole functionality.
Method Explanation:
div
is created such that it will always display only one number by setting its height and width as 1em. Overflow of thediv
is set to hidden so as to make only one line visible.div
aspan
containing all the numbers from 1 to 0 is created and is positioned relative to the parent.span
is at 0px or 0em but during theanimation
the top position is changed so that thespan
gives the impression of being moved upwards. Because thediv
can display only one number at a time, it gives the spinning effect (or the effect of the other numbers disappearing).span
elements. 0em means the 1st line is visible (number 1), -2em means the third line becomes visible (number 3) and so on.Note: Using this method, the spin will look like going the whole circle every-time and not like the JSBin sample in question where a 3 to 4 for the first digit would be just a single step and not a full circle.
The below is a sample with JavaScript where the animation effect is achieved through CSS but the triggering of the animation and the setting of the endpoint is achieved using JavaScript.
The JS code is pretty much self explanatory. All we are doing is the following:
animate
class to all thespan
elements which are part of this animation effect. This is because we want the animation to happen only when the button is clicked.top
property of eachspan
to some random number. This would mean that each span will have a different number displayed.animate
class so that when we click the button again the animation can start all over again.Version 2: (with only a number increment effect instead of a spinning effect)
This is created using largely the same code as the previous one but unlike the previous one where the
animation
makes the movement from one number to another a gradual one (and thereby producing a spin effect), here the animation makes the movement more like a sudden jump from one number to another and thereby produces just an increment effect.The jump is achieved by maintaining the
top
position the same till its just about time to move to the next (that is, thetop
is set to be0em
till 9.9% of the animation but at 10% it suddenly changes to-1em
).