Vue 3 Animations Tutorial #10 - Using GSAP

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Introduces GSAP for animating Vue 3 components in JavaScript hooks

Full Transcript

all right and gang so now we know how these javascript hooks work let's try using them to control our transitions instead of using css so i'm in the about component first of all and what i'm going to do is delete all of these css transition classes that we have we don't need those anymore and up here in setup i've just got two hooks before enter and enter and they're the two hooks that we're going to need to create an enter transition and that's also all i return at the bottom so i deleted everything else and then up here on the transition component we have just those two hooks and then i can delete this name right here if i wanted to because we're not using those transition classes anymore but i'm going to keep this right here and that means that when we first see on the page it's going to transition in so we could create all of these animations using vanilla javascript and no extra library but it would take a fair amount of time to code and a lot of trial and error so instead we're going to be using a javascript animation library called gsap gsap is a package that we can install into our project with npm and it works really well with the transition hooks that vue gives us and gsat provides us with a load of methods then that we can use on elements to animate them from one state to another so if you want to learn more about gsap go to greensock.com forward slash gsap and you can read all about it here for now though let's try installing this into our project so then the first step is to install gsap into our project so let's open up a new terminal and say npm install gsap like so and now that's done we can use it inside this project the first thing we need to do is import it so i'm going to say import gsap from gsap which is now stored away in our node modules folder and now we have this we can use it inside any of these hooks right here now the way we do this is by first of all setting the initial state of this element that we're transitioning and we do that in before enter this is a bit like the class enter from we set the initial state where do we want to start so we can do that inside before enter and we don't need to use gsap in order to do this because we're not animating at this point we're setting things up so let me just log a message to the console here console.log and i'm going to say before enter and then hyphen set initial state so this is where we set the initial styles if you like of the element we're transitioning in so in order to do that i can take the element i can say dot style and then apply whatever styles i want to it now to begin with i'm going to say transform and i'm going to set that equal to trans late y minus 60 pixels and basically this means that it's going to start a little bit higher and then eventually it's going to come down onto the page now the other style i want to apply is an opacity so l dot style dot opacity and set that equal to zero to begin with okay so they're the initial styles now i want to transition to the styles that they're going to be on the page and that's where we use gsat because if we just said right here l dot style dot opacity is equal to one for example it's not gonna animate from zero to one it's just gonna click into position when it enters the dom and there's gonna be no transition we want to animate it so in order to do that we use gsap so let me do another log to the console first of all i'm going to say starting to enter hyphen make transition all right then so we'll say gsap and then use a method called two and this is us saying we want to animate to something or other now it takes two arguments the first one is the element that we want to animate in and we get access to that right here the second argument is an object and in here we define the different css properties that we want to animate so i could say y to zero and this is a transform we don't say transform translate y we just say y and this does a transform for us under the hood so it's going from minus 60 to zero the other one is opacity and we want that to be one now the other thing we can do in here is set a duration so i'm going to do that as well duration this is how long it takes and we'll just say it takes one second all right then so if i save this now and come over here then we can see it comes in from the top and it fades in as well so that was quite nice now one thing we can do is add other easing functions to this and we get more with gsap so one for example is bounce dot out and this gives us kind of like a bounce effect so if i save this now and preview i'm going to refresh so we can see that now we get that bounce effect that looks quite nice cool so that's how simple it was to use gsap right here to animate in from an initial state to this state right here now one more thing i want to show you i'm gonna create the next hook so const and after enter set it equal to a function like so and i'm just gonna console.log after enter like so and i also need to return this at the bottom over here so after enter and i'm gonna attach the after enter hook right here as well so after hyphen enter like so set it equal to after enter all right then so this should fire right here after it fully animates in right so to demonstrate this let me just change this duration to three seconds i'm going to save it and i'm going to come over here and i'm going to refresh now notice the hook fires but it's still transitioning in and it should only fire after the animation is complete but the problem here is that the transition component doesn't know when this animation is complete we say the duration is three seconds but this is separate from the transition components we need to tell view when this is done now in order to do that we can use a second argument that we get access to and that is a function that we're going to call done and this is a function that we can invoke to tell view that we've done with the transition now we do that using an on complete property inside this gsat method so this oncomplete property is going to be a function that will fire once this animation is done after three seconds and when we fire that function it tells you that we're done with this stage and then we go on to the next stage so now hopefully this should only log after these three seconds so let me save this and you can see now it waits until it's fully transitioned in and then fires after enter so now we know how to use gsap next up we're going to make a more complex staggered animation for this contact page right here

Original Description

Hey gang, in this Vue animations tutorial I'll introduce you to GSAP, wih will give us an easy way to animate our Vue components in JavaScript hooks. 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/vue-animations 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Helpful Links: + Vue 3 Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9hYYGbV60Vq3IXYNfDk8At1 + HTML & CSS Course - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + Modern JavaScript course - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + Get VS Code - https://code.visualstudio.com/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
I let an AI write 6 WebGL hero sections — here's the one-CSS-variable reskin trick, and the honest review count
Learn how to reskin WebGL hero sections using a one-CSS-variable trick and discover the author's honest review of AI-generated code
Dev.to · NeroTran
📰
Real-Time Rails Without Turbo: Modern Reactive UIs with Inertia and DexieCable
Learn how to build real-time Rails applications without Turbo using Inertia and DexieCable for modern reactive UIs
Dev.to · Stefan Buhrmester
📰
The Frontend Build Wars: Why Webpack’s Architecture Collapsed and How Rust Took Over
Learn how Webpack's architecture collapsed and how Rust is taking over in the frontend build wars, and why 86% of developers still rely on outdated tech
Medium · JavaScript
📰
Persisting User Data in a No-Backend HTML App (localStorage Patterns)
Learn how to persist user data in a no-backend HTML app using localStorage patterns
Dev.to · PromptMaster
Up next
Best Free Loading Animations for Web Developers
Alamin
Watch →