# How to keyframe a slider control in After Effects

Lets say i want to animate a number using a slider control and I want the number to be "1" on the first keyframe and on frame number 20 i want it to be "2". Now if I place a keyframe on frame number 20, the number will change instead on frame number 10 as it is the halfway point. Intuitively I was expecting the animation to start counting the decimals after the first keyframe and finally on the last, after reaching 1,9 it would turn into 2 exactly on frame number 20. However it seems this isn't the case. I'm trying to use this method for animating a date counter on a historical video and it's important the date, lines up exactly with what's happening on the video. And constantly moving keyframes up and down will take a long time. Is there any other solution, or am I missing something.

The value of the text will recalculate on every frame, based on the value of your slider at that time, so if you've set the following values:

``````frame = 1  slider = 1
frame = 20 slider = 2
``````

then the value of the slider at frame 10 will be 1.5

On frame 19 it will be 1.95 as that is the correct linear calculation of that value at that time.

It might be easier if you make a new answer explaining exactly what you want to achieve.

If you want only a single decimal place, you could do that like this:

``````slider = (effect("Slider Control")("Slider").value);
output = (Math.round(slider * 10))/10;
SourceText = output
``````

The Math.round function (note the annoying capital 'M'), causes the number to round to the nearest whole number. Because you want decimal places, we multiply it up by ten inside the function, do the rounding, then divide it again after the rounding. Think of the number of zeroes in that line as the number of decimal places you want, ie if you wanted two decimal places, change the '10' values both to '100'.

This will give you:

``````Frame = 1  text = 1.0
Frame = 10 text = 1.5
Frame = 19 text = 1.9
Frame = 20 text = 2.0
``````

If you wanted to keep the value rounded down until you hit frame 20, use Math.floor like this:

``````slider = (effect("Slider Control")("Slider").value);
output = (Math.floor(slider));
SourceText = output
``````

Which gives:

``````Frame = 1  text = 1
Frame = 10 text = 1
Frame = 19 text = 1
Frame = 20 text = 2
``````

Or if you wanted to keep it rounded down to one decimal place:

``````slider = (effect("Slider Control")("Slider").value);
output = (Math.floor(slider *10))/10;
SourceText = output
``````

Gives:

``````Frame = 1  text = 1.0
Frame = 10 text = 1.5
Frame = 19 text = 1.9
Frame = 20 text = 2.0
``````

You can also use the Toggle Hold function in your keyframes by right clicking a keyframe, and selecting 'Toggle Hold', which will hold your slider at a specific value if you need to.

In general, every time I'm working with sliders for text expressions, I start with this expression:

``````slider = (effect("Slider Control")("Slider").value);
tidying = (Math.round(slider * 100))/100;
SourceText = "\$" + tidying + "bn" +"\r" +"DEBTS";
``````

Which usually gets me close to what I need - it allows for rounding to two decimal places (the 100 values do this), text before the value (in this case a dollar sign), text after the value ("bn"), and a new line (created using "\r"), and text on the line beneath it ("DEBTS").

I think there are neater ways to do this now - take a look at these rounding techniques, but this is the way I usually do it.

• This answers and explains it beautifully thank you. In the end I ended up adding one decimal point so that the number wouldn't round up prematurely. And to hide the decimal I just used a mask. My problem in this case is that my video is about a historical event and in my animation, I need the date to turn from 1520 to 1521 at an exact point in the animation. So when I put the keyframe in the exact location the number changes too early. Unfortunately the 'Numbers' effect doesn't help me because I can't set it back in time earlier than 1910. I'm gonna look into the things you sent. Commented Apr 14 at 19:57
• use two hold keyframes on the two frames where you need them to switch - ie one at 1520, and one at 1521
– tomh
Commented Apr 15 at 11:46