
Quick Lecture Overview
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
In GSAP, one of the most fundamental and powerful methods you'll use is .to(). Think of it as telling your element: “Hey, go from where you are right now… to this new state.” With .to(), you're defining what the end result should look like.
And GSAP takes care of animating from the element’s current position or style to the new values you provide. For example, if you want a box to move to the right and fade out, you’d use .to() to animate its x position and reduce its opacity.
Here’s a simple example:
It's like giving GSAP a destination, and it figures out the smoothest way to get there. Whether it's changing position, scale, rotation, color, or any other animatable CSS/JS property, .to() is your go-to method. And since it starts from the element’s current state, it makes chaining and sequencing super flexible - no need to manually reset or define where the element begins. You just say where you want it to go, and GSAP takes the wheel.
And just like that... your box moves, right ?
We’ll explore all of these superpowers very soon. 🦸♂️
👉 Try animating your not just to the right, but also make it fade out (opacity: 0) at the same time!
gsap.to(".box", {
x: 200,
opacity: 0,
duration: 1
});
Your box will move right and vanish! Magic. ✨
So far, we animated just x and opacity.
But can handle so much more — and it's not just about CSS!
Here’s a sneak peek at what you can animate with :
This is from the gsap cheatsheet cheatsheet
gsap.to(".selector", {
x: 100, // Move horizontally
backgroundColor: "red", // Change color (camelCase!)
duration: 1, // How long the animation takes (seconds)
delay: 0.5, // Start after 0.5 seconds
ease: "power2.inOut", // Control the speed curve
stagger: 0.1, // Animate multiple elements with a delay between them
paused: true, // Start paused (you can play later!)
overwrite: "auto", // Manage conflicting animations
repeat: 2, // Repeat the animation 2 times (-1 = infinite)
repeatDelay: 1, // Wait 1 second between repeats
repeatRefresh: true, // Refresh values on each repeat
yoyo: true, // Reverse the animation (A-B-B-A)
yoyoEase: true, // Different ease when reversing
immediateRender: false, // Control when the animation starts calculating
onComplete: () => {
console.log("finished!");
},
// Other callbacks: onStart, onUpdate, onRepeat, onReverseComplete
});
In this lesson, you learned how gsap.to() lets you animate elements from their current state to a new one — whether it's moving, fading, scaling, or rotating.
With just a few lines of code, you can already make your UI feel smoother and more dynamic.
What if you want to animate something starting from a specific state instead?
That’s where comes in. Get ready to flip the animation flow — starting from your custom starting point!
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.
var
(function-scoped, outdated)let
(block-scoped, modern and recommended)const
(block-scoped, cannot be reassigned)_
, or $
let let = 5;
is invalid)myVar
and myvar
are different)string
, number
, boolean
, null
, undefined
, bigint
, symbol
Objects
, Arrays
, Functions
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
00:00:00 Now, let's dive a bit deeper into a method that we have used most often in this entire course.
00:00:07 It is the gsab.to method used to animate the elements from their current state to a new state.
00:00:14 We'll learn it on an example of animating a toast notification.
00:00:19 So, to get started, head over to Pages and find the Learn To.
00:00:24 you'll see that I added some simple HTML, which just contains this toast notification, and then in the styles, I simply styled it in a simple way.
00:00:34 But then, where the magic happens is within the script.
00:00:37 Now, if you head to our current application, you'll notice that right now, it is completely static.
00:00:42 So one thing that I want to start with is just modify some default styles to minus 100px, which will move it off the screen,
00:00:51 and then I also want to change the opacity to zero.
00:00:53 Because now, within the script, we'll animate it.
00:00:57 First things first, let's import GSAP from GSAP.
00:01:01 And then we can create a function.
00:01:04 So const show toast loop, because it'll loop it just so we don't have to repeat it every now and then.
00:01:10 And we can just open up a new function within which we'll do the animation.
00:01:15 So you could have totally just said gsap.to right here in the global object.
00:01:20 But in case you want to reuse it, then it might be good to put it within a function.
00:01:24 So I'll make a function call as soon as the application loads, showToastLoop.
00:01:29 And then within it, you'll want to start animating it by saying gsab.to, target d.toast, and then first of all, move its y position to minus 120. This
00:01:42 will move it upward.
00:01:44 And don't forget to change the opacity to one.
00:01:47 Immediately, it'll pop up.
00:01:48 Now we can also modify the scale to something like one, because initially, if you check out the styles, you'll see that its scale is set to about 0.95.
00:01:59 So a bit smaller than one.
00:02:00 So if we increase it to one, it's going to make it appear like it's growing as it's coming up.
00:02:04 We can also change the duration to like 0.8 seconds to make it slowly appear.
00:02:11 Or in this case, I would say emerge.
00:02:14 And you can choose the easing.
00:02:16 Any easing will work here.
00:02:17 I'll go with power4.out, which will make the motion feel fast at the start, but then smooth at the end.
00:02:24 Like it ramps up the speed, but then finds the place where it wants to stay.
00:02:28 And we can call it a day.
00:02:30 This is it.
00:02:30 Whenever you reload the page, it'll emerge.
00:02:33 But we've covered GSAP too so many times, so in this lesson, I want to teach you something extra.
00:02:38 Every single GSAP method comes with something known as callbacks.
00:02:43 All tweens and timelines have different callbacks.
00:02:46 A quick look at GSAP's documentation page on callbacks will show you there are quite a lot of different ones, like onComplete,
00:02:53 onStart, onUpdate, onRepeat, and onReverseComplete.
00:02:58 And the way you call them is you simply pass them as an additional method, as a property to one of the methods you've used before.
00:03:05 Let me show you how that works in action.
00:03:07 For example, when you want to rerun the animation at the end.
00:03:10 So in this case, we'll use the onComplete callback.
00:03:15 And then within it, you want to say what you want to happen once this animation completes.
00:03:20 Such as maybe you want to apply an additional animation by saying gsab.to.
00:03:26 We can once again target the toast and then apply a delay of about 2.5 seconds.
00:03:32 And then here we want to pause the animation by applying a delay of about 2.5 seconds, which will make the toast stay on the screen for about 2.5 seconds.
00:03:43 And then after that, we want to slide it out by setting the Y to zero.
00:03:50 We want to change the opacity back to zero.
00:03:53 We also want to change the scale back to 0.95 throughout iteration of about 0.7 seconds with an ease of something like power2.in.
00:04:07 So if you save it, it's going to come up, wait about 2.5 seconds, and then it's going to hide itself.
00:04:13 The use of gsub2 and the onComplete callback was the perfect use case for how toasts typically work.
00:04:20 They show up, they tell you that you have about 99 problems, and then they go away.
00:04:25 And if you want to, you can even chain the callbacks.
00:04:29 So if you want to apply a callback to this GSAP2, you can totally do that by saying onComplete.
00:04:35 In this case, we can just restart the animation after a slight delay.
00:04:39 For example, by calling a regular JavaScript method of setTimeout, which will recall this showToast loop.
00:04:47 after a specific number of milliseconds, like 2.5. So now if we save it, it's going to be here, it's going to go away, and after two and a half seconds,
00:04:58 it's going to repeat.
00:04:59 And with that, you've learned how to use the GSAP2 method.
00:05:03 Technically, you've known it for this entire time, but hey, the GSAP2 method deserved its own lesson too.
00:05:09 So here you have it.
00:05:11 Congrats.