Last active
October 28, 2024 09:54
-
-
Save CoskunCakir/d594995a9a2afdf3a16b1feb6cd1e90a to your computer and use it in GitHub Desktop.
Angular fade animation for transition between routes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
trigger, | |
animate, | |
transition, | |
style, | |
query | |
} from '@angular/animations'; | |
export const fadeAnimation = trigger('fadeAnimation', [ | |
transition('* => *', [ | |
query( | |
':enter', | |
[ | |
style({ | |
opacity: 0, | |
position: 'absolute', | |
height: '100%', | |
width: '100%' | |
}) | |
], | |
{ optional: true } | |
), | |
query( | |
':leave', | |
// here we apply a style and use the animate function to apply the style over 0.3 seconds | |
[ | |
style({ | |
opacity: 1, | |
position: 'absolute', | |
height: '100%', | |
width: '100%' | |
}), | |
animate('0.3s', style({ opacity: 0 })) | |
], | |
{ optional: true } | |
), | |
query( | |
':enter', | |
[ | |
style({ | |
opacity: 0, | |
position: 'relative', | |
height: '100%', | |
width: '100%' | |
}), | |
animate('0.3s', style({ opacity: 1 })) | |
], | |
{ optional: true } | |
) | |
]) | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import { fadeAnimation } from './route.animations'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
animations: [fadeAnimation] | |
}) | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div [@fadeAnimation]="o.isActivated ? o.activatedRoute : ''"> | |
<router-outlet #o="outlet"></router-outlet> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment