Here I'll just talk you through it briefly. We've got our menu open state, which is either true or false. Then here, this is our menu, which we're using the vshow directive so that when the ref is true, then the menu is open, otherwise it's not added to the DOM.
And you'll see we've got this transition component wrapped around it. So what this does is this is a built-in component in Vue. This means that when the state changes, when the menu open changes from false to true, it isn't just added to the DOM, it is added to the DOM, but it's got a bunch of classes added to it, which you can use to, in this case, add a CSS transition even.
So here, what we can see is this class is, so when this element is added to the DOM, it's added with this enter from class. So it's got a CSS transform, which translates it to the right by 100%, which in this case would be the width of the menu. So effectively, when it's just added to the screen, the menu is just off screen here. So, you can imagine it's sat slightly off screen. You press the button, it comes in on screen. That's what this translate means, translate x 100%, translate it in the x direction, 100%, which in this case is 100% of the width of this.
So, the next thing Vue does once it's added this class and added this element into the DOM, is it adds this class called interactive, which we used to add a transition, which we use a CSS transition, and then it removes this class so the new transform will be unset, so effectively, it will be translated nowhere. So it will be its natural position in the DOM, which is here. So what the CSS transition property does is, in this case, we're saying transition the transform property, take 0.3 seconds to do it and use this easing. I'll get back to easing in a second. But effectively, what this does is you can give it quite a few CSS properties, and it means that when it changes, don't change it immediately, transition it from the old value to the new value in this time. So in this case, we're saying we want our CSS transform, which is going from translation to the right to nothing, because the class is being removed, and taking 0.3 seconds to do it. So the explanation is a little convoluted. But hopefully in the context of seeing what the menu does, that makes sense. That's the enter class. We've got a slightly different class for leave. So you can just combine these into one depending on what you want to do. So you'll see on some of my later examples, I'll have enter active, and leave active in the same rule. And then I'll just have this, the transform and 0.3 seconds. In this case though, we have some easings. So we've got ease out on the way in, and ease in on the way out. And that won't make sense if you don't know what it means. So I will show you. So if you watch this menu come in and out, you'll see when it goes out, it starts off moving slowly, and then it speeds up.
Comments