I started writing some JS code to cause a variables value to increase over time, up to a target value, with some form of ‘ease-in’.
I realised that jquery already does this in it’s .animate() method. Of course, the method is for manipulating CSS properties, not general variables.
My question is, is there anything that can be done to hack it so that the method affects a variable, rather than a CSS property?
Yes, you can animate variables. Demo here
In this example, I am animating n from 0 to 10 in 1 second. The
stepfunction is called during animation and from there you can retrieve the current value innow.Personally, I used this technique to animate several css properties simultaneously in a non linear fashion.
Animate runs by modifying the value of properties declared in JS objects. Although
animateis designed to change CSS scalar values, it can also safely be used for any generic property, as longvalueis a scalar one.In fact, you can think of CSS as a set of JS objects, where properties are for example,
top,marginetc.Note that the following scripts do the same. They change CSS
leftfrom 0 to 10is the same as
or, without using the
nowparameterTo see them in action click here