Skip to main content
View on YouTube to comment and like!

Syntax Video

View Transitions Are More Powerful Than You Think

Chapter 1 00:00

Transcript

view transitions in the browser are more powerful than you may think in fact I think they're extremely underrated often times when we talk about view transitions the way that you're seeing them use the most is what like a fade on page change or one item zooming from here to there or even some more complex animations but we're often thinking about view transitions being used as page changes if we're thinking about view transitions at all but did you know that you can use view transitions in a bit more of a flip animation style way I'm going to show you a quick example here about how I used view transitions to write this animation that you're seeing right now that's right every single animation that you're seeing being done here is being done with a Simple Start view transition a view transition name and adding and removing things from the Dom that's it there's no additional CSS or JavaScript that's having these things zoom into place or

fade out when they're done so let's dive into this code and check it out so this is a game I threw together in a couple hours on the airplane and one of the things that made this game easy to do was the view transitions here specifically because it added a level of polish that the game didn't have when things were just snapping into place so when we think about view transitions often times again we're thinking about page changes in transitions but often times what we need for a good view transition is really just a unique name in inside of a transition name and a start view transition call in JavaScript and that can take place on any event all right now let's take a quick at a really simplified version of the same thing you remember that all we need to have is a view transition name that is unique and for something to be removed from the Dom and then added somewhere else okay so in this example I have a card that is being removed when I click toggle and another card that is being added when I click

toggle since they both have the class name of card they're both receiving the same name of view transition name card okay there's only one on the page at a time that's perfect for us next I have the second card has been given some unique CSS that actually positions it down here so you'll see when I click this snaps back and forth right that's to be expected now how do we animate this to do that all we need to do is find the section of the code that's modifying the Dom which is our onclick and we'll want to say document. start view transition and then we're going to wrap

this like this now just between that act of having start view transition and view transition name check this out nice it's perfect right and we can do anything we want to the second state and have the values interpolate correctly so we could say scale of three and let's do a background of red or something something crazy right that way you can truly see this and whoop whoop whoop perfect this is so neat because again we're just taking something and removing and adding it and the browser is handling the rest I don't have to bring in an an animation library to do flip transitions I don't have to write any JavaScript to calculate the end position and make that happen and it's not happening on a page route change again this is really just wrapping a a single toggle change in a start view transition now you may have noticed that this is taking place

because I have card 2 with some custom CSS but this will still work if let's say button one is in a div over here and button two is in a footer over over you can't see my hands fo they're all they're all in different places in the Dom right so what if there's no special CSS applied and they're just in different places in the Dom if you do this it will still interpolate the position between where they want to go and in fact that is what I'm doing within my cards example so now knowing what we know there we just take a look at every single one of these cards has a unique name when I click a card it is removing it from the Dom right here adding it to the Dom right here but they have the same unique name the one that remains here is the ghost card it actually has a unique name as a ghost card so therefore it's not actually connected then when all of the cards are played they maintain their unique names

in a stack of cards that is actually a physical stack of a bunch of divs right because I wanted them all to go into that end place and then they're being removed from this position to the Dom added in this position of the Dom and they're quite a bit different looking right and it all interpolates correctly where it needs to go every single one of the transitions is being done using that exact technique I showed you in this felt repo pretty pretty sick I'll have that code available to you in the description there's nothing spel specific that makes this work in particular uh anytime you're adding and removing things from the Dom you can call that start view transition and you're going to get that interpolation the things that I see go wrong with this the most is not having a unique name that is the same as the item being added and removed also if you have a duplicate name let's say two items in the Dom have the same unique name uh the browser will complain and you will see a console

error when you try to do this transition okay so make sure that you're checking your console for any type of errors that might be popping up to help you with this but as long as it has those two names you get a nice view transition so this is pretty neat right but you can also do view transitions for so much more if you want to see more interesting view transition techniques hit subscribe bring the bell all that stuff what are you doing a view transitions that are cool is there anything that is super exciting to you are you skeptical of this API let me know in the comments below as always this is Scott with syntax and I'll see you in the next one

Quote it with credit and a link. Quoting Syntax