zagoa
zagoa

Reputation: 828

Angular 5 animation specific transitions

So my problem is I want to specify a specific animation between to pages.

I want to do one sort of animation when I click on a button which navigate to a specific URL(I am on page1 and navigate to page2) http://localhost:4200/page2. Then if I am on page1 and want to do goBack() I want a different animation when I return on the home page http://localhost:4200.

But with the :leave option of transition I can't achieve my goal, because I will have just one type of animation. My idea is to do something like that:

trigger('zoomAndFade', [
  transition( 'page1=> page2', [
    query('.content', [
      style({fontSize: '1em', opacity: 1}),
      animate('2s ease-in-out', style({fontSize: '1.5em', opacity: 0}))
    ])
  ])

trigger('fade', [
  transition( 'page2=>page1', [
    query('.content', [
      style({opacity: 1}),
      animate('2s ease-in-out', style({opacity: 0}))
    ])
  ])

but how can I specify the state of a page to the animation ?

Upvotes: 0

Views: 1254

Answers (1)

user9112752
user9112752

Reputation:

This is possible. Just add this getState methode to your app.component.

app.component.ts

export class AppComponent {
    getState(outlet) {
        return outlet.activatedRouteData.state;
    }
}

In your template, you need to get the new state of the router on a change event.

app.component.html

<main [@routerTransition]="getState(o)">
    <router-outlet #o="outlet"></router-outlet>
</main>

Now the animation in you component can distinguish between your pages.

Then you can have your transistions

trigger("routerTransition", [
    transition("* => settings", slideLeft),
    transition("settings => *", slideRight),
])

But your router needs to transmit data

{
    path: "settings",
    component: SettingsComponent,
    data: { state: "settings" }
}

And you animation can now feature :enter and :leave

query(":enter",
    [
        style({
            transform: "translateX(100%)"
        }),
        animate("0.4s ease-in-out",
            style({
                transform: "translateX(0%)"
            })
        )
    ], {
        optional: true
    }
),
query(":leave",
    [
        style({
            transform: "translateX(0%)"
        }),
        animate("0.4s ease-in-out",
            style({
                transform: "translateX(-100%)"
            })
        )
    ], {
        optional: true
    })

package.json

{
  "name": "health",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.2.4",
    "@angular/common": "^4.2.4",
    "@angular/compiler": "^4.2.4",
    "@angular/core": "^4.2.4",
    "@angular/forms": "^4.2.4",
    "@angular/http": "^4.2.4",
    "@angular/platform-browser": "^4.2.4",
    "@angular/platform-browser-dynamic": "^4.2.4",
    "@angular/router": "^4.2.4",
    "core-js": "^2.4.1",
    "rxjs": "^5.4.2",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.4.8",
    "@angular/compiler-cli": "^4.2.4",
    "@angular/language-service": "^4.2.4",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.2.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.3.3"
  }
}

Upvotes: 1

Related Questions