Download Project File

How To Create a Hover Button Effect Using Expressions in After Effects

In this post, you will learn how to create a hover button effect using expressions in After Effects.

Alright, so we are in After Effects, now let me show you what we are going to be creating today.


So we have a CLICK HERE button along with a cursor. Watch what happens when I take my cursor and hover it over the button.

Cursor hovered over a button in After Effects

As you can see, two things changed. The shape of my cursor is no longer an arrow. It is now a hand. Also, the color of my button is now red instead of blue.

Pretty cool, right?

So if I move the cursor away from the button, the hand will switch back to the arrow and my button will change back to blue instead of red.

back to blue in After Effects button

Now let me show you how to set it all up using expressions in After Effects.

The Setup

So here is my setup in After Effects.

The Setup in After Effects

I have a Source Text, which I will use for the expressions portion of this post. I can show you visually what I am doing with expressions.

Then I have a CTRL null that controls both of my cursors.

Control Null in After Effects

So both the MOUSE ARROW and MOUSE HAND are parented to the CTRL null.

Control Nulls Parented in After Effects

Next, we have the CLICK HERE text layer.

Click here text in After Effects

And lastly, we have the shape of our button.

Shape of our button in After Effects

Activating Expression

I am going to ALT+RIGHT CLICK (OPT+RIGHT CLICK on a Mac) on the stopwatch icon of my Source Text.

Stopwatch icon to active the expression in After Effects

Next, I am going to define some variables.

Text Editor Expressions After Effects

So I am going to say point, you are going to be the position of our CTRL layer.

CTRL control in After Effects

Position CTRL After Effects

point = thisComp.layer(“CTRL”).transform.position;

Next, we are going to definitely our shape.

I am going to type shape = and then I will pick whip to the ORIGINAL SHAPE layer of our button.

Shape After Effects

Shape pickwhip after effects

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

Sample Image Expression

Next, we are going to use the sample image expression.

So I am going to say shape.sampleImage(), and we are going to definite our point inside of the parenthesis along with the range. So I am going to say the sample is going to be between .5 and .5.

Sample and Range after effects

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

shape.sampleImage(point, [.5, .5])

Now watch what happens, so right now we have 4 different numbers in my composition above our button. They are
all zeros.

All zeros in After Effects

However, as soon as I move my null and if I hover over my shape button, you can tell that everything has changed.

Everything changed in After Effects
Now we have different numbers but the last number went from 0 to 1. So that is the one I am after and I am pretty sure it is the Alpha Channel.

So how do I get only this last number? Let me show you!

I am going to grab it by using an index. I am going to say give me the index number 3 because in After Effects we count our index starting at 0. So our fourth number is really 3. Because we start at 0 and then we go to 1, 2 and then 3. I hope that makes sense.

With that said, inside these square brackets, I am going to say 3.

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

shape.sampleImage(point, [.5, .5])[3]

And if I let go, you can see that it gives me 1 when the cursor is over the button.

Gives me one in After Effects

It will give me 0 if the cursor moves away from the button.

Moving the mouse away in After Effects

That is exactly what I am after here but I want to convert that 1 to 100. So instead of it going from 0 to
1, I want it to go from 0 to 100.

So I am going to give it a variable here and I am going to say A you are going to equal to this expression: shape.sampleImage(point, [.5, .5])[3].

A equals to an expression in After Effects

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

a = shape.sampleImage(point, [.5, .5])[3];

Next, I am going to do a linear expression, so I am going to say linear() and then in my parentheses, I am going to say A, I want the number from variable A starting at 0 to 1 and when it is a 0 I want it to be 0 and when it is 1, I want it to be a 100.

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 0, 100)

So if I let go, right now I am getting 0 when the cursor is not on top of the button.

Getting 0 in After Effects

But if I hover over my button, it went from 0 to 100. So that is exactly what I want.

Hovered over a button in After Effects

Next, I am going to select the text and copy it.

Select and copy text in After Effects

Now I am going to go inside my MOUSE ARROW by first selecting it and then pressing T on the keyboard to reveal the opacity.

Reveal Opacity in After Effects

Click on the stopwatch icon to create an expression.

Create an expression stopwatch icon in After Effects

And then I will paste that expression inside the text editor.

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 0, 100)

Paste the expression in After Effects

So if I let go, you can see that my arrow is sitting on top of my hand cursor.

Arrow over top of hand in After Effects

But when I pull it away from the button, the arrow disappears.

Arrow disappears in After Effects

We need to reverse that effect. I want the arrow to be visible when it goes over my button and I want it to disappear when it is not over my button.

So to do that, I am going to change a 0 to 100 and 100 to 0.

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 100, 0)

Okay, so now everything is in reverse. We see the arrow when the cursor is not over the button.

See arrow in After Effects

And we see the hand when the cursor is on the button.

Hand in After Effects

So that’s good. Next, I am going to select my MOUSE HAND and hit T to reveal the opacity.

Mouse Hand in After Effects Opacity

Then I will ALT+CLICK (OPT+CLICK on a Mac) on the stopwatch icon to activate the expression.

Activate expressions with stopwatch icon click in After Effects

And we are going to paste the expression in here again

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 100, 0)

Paste expression in After Effects

This time we do not have to reverse anything because this expression does exactly what I want it to do.

Paste expression in After Effects

So as you can see, the hand cursor disappeared when I am not hovering over the shape.

Disappeared when not hovered over the shape layer in After Effects

But if I go over the shape, the arrow goes away and the hand appears. That is exactly what I want.

Now let’s work on changing the color of the shape.

To do that we first need to select the ORIGINAL SHAPE layer.

Original Shape in After Effects

Then press CTRL+D (CMD+D on a Mac) to duplicate the ORIGINAL SHAPE layer.

Duplicate in After Effects

Change the name of it to BG for background.

Change the name to BG for background in After Effects

I do not want to use the ORIGINAL SHAPE. I only want to use that for the sample image. So I do not want to touch it.

Now I am going to change the color of this BG shape. I am going to go into Effects and Presets and grab Fill and then drop it over the top on BG.

Grab fill and place it on BG in After Effects

We are going to change the color of our shape.

Changing color in After Effects

Changing color in Adobe After Effects

So we have the blue color and we want it to switch from red and blue.

I am going to select the BG shape layer and then hit E to reveal the effects.

Select BG layer in After Effects

Then I am going to go inside Fill, select Opacity and hit S twice to solo Opacity.

Select opacity and solo it in After Effects

Solo in After Effects

I am going to ALT+CLICK (CMD+CLICK on a Mac) on the opacity stopwatch icon and just paste our expression inside the text editor.

point = thisComp.layer(“CTRL”).transform.position;

shape = thisComp.layer(“ORIGINAL SHAPE”);

a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 100, 0)

Activate expression in Adobe After Effects

Paste after effects expressions

That is pretty much it! So now if I drag my arrow over the button, the button will automatically change from red to blue and my arrow cursor will switch to hand.

Curser swtiched to hand in After Effects

I hope you found this post useful. If you would like to learn more about Expression and how it can speed up your workflow in After Effects, check out my Learn After Effects Expressions Course.

Download Project File

About the author:

Sergei Prokhnevskiy

Sergei Prokhnevskiy is a Charlotte, NC based motion graphics designer, an online tutor, an entrepreneur, and Adobe MAX Master Speaker. Sergei is known for his quick and practical After Effects tips and tricks on YouTube, where he generated well over 6 million views on his tutorials. He has authored an online video course on After Effects Expressions and developed scripts for After Effects. He is also a former senior motion graphics designer at Fox Sports where he worked on graphics for NFL, Super Bowl, MLB, World Series, College Football, NASCAR, Women’s World Cup, and NASCAR Race Hub. He is very passionate about learning and teaching others.

5 FREE TIME-SAVING TOOLS

Over 100,000 downloads & counting!

These 5 After Effects tools will seriously speed up your workflow and save you hours of wasted time. More than that, they could be the difference between being able to complete a project on time or not!