Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Simple Angular2 component to create a page transition animation on route changes

License

Notifications You must be signed in to change notification settings

bergben/ng2-page-transition

Repository files navigation

Join the chat at https://gitter.im/bergben/bergben

ng2-page-transition

Simple Angular2 component to create a page transition animation on route changes.

Works for Angular 2.x only. Angular 4.2 supports route transitions built in:

https://github.com/angular/angular/blob/master/CHANGELOG.md#420-rc0-2017-05-19

Check out #8 for more info.

Features

All that this component does is listen for route changes and on NavigationStart it triggers an animation to fade out the current page and fade in the page on the new route. By default it also scrolls the page to the top on route changes, this can be disabled though.

Install

$ npm install ng2-page-transition --save

Add web-animations-js if you haven't done so already to support all browsers (see https://angular.io/docs/ts/latest/guide/animations.html)

$ npm install web-animations-js --save

Usage

Import the module

// app.module.ts
import {NgModule} from '@angular/core';
import { RouterModule } from '@angular/router'; //Router is required for the component to work
import {BrowserModule} from '@angular/platform-browser';
import { Ng2PageTransitionModule } from "ng2-page-transition"; // <-- import the module
import {MyComponent} from './my.component';

@NgModule({
    imports: [BrowserModule,
              RouterModule.forRoot() //Router is required
              Ng2PageTransitionModule // <-- include it in your app module
             ],
    declarations: [MyComponent],  
    bootstrap: [MyComponent]
})
export class MyAppModule {}

Wrap content in your template

<!-- my.component.html -->
<ng2-page-transition>
    <router-outlet></router-outlet>
    Some other content
</ng2-page-transition>

Options

scrollTop

By default the component scrolls to top on route changes, you can disable this by setting [scrollTop]="false"

<!-- my.component.html -->
<ng2-page-transition [scrollTop]="false">
    <router-outlet></router-outlet>
    Some other content
</ng2-page-transition>

onlyOnRoutes

By default the transition will take place on any route change. You can activate the transition only on routes that contain one or more certain strings:

<!-- my.component.html -->
<ng2-page-transition [onlyOnRoutes]="['blog']">
    <router-outlet></router-outlet>
    Some other content
</ng2-page-transition>

onlyOnRoutes takes an array of strings, in the example above the transition would only happen on any route containing "blog" in the url.

ignoreOnRoutes

By default the transition will take place on any route change. You can deactivate the transition for routes that contain one or more certain strings:

<!-- my.component.html -->
<ng2-page-transition [ignoreOnRoutes]="['blog']">
    <router-outlet></router-outlet>
    Some other content
</ng2-page-transition>

ignoreOnRoutes takes an array of strings, in the example above the transition wouldn't be triggered on any route containing "blog" in the url.

Custom transition

If you want a different animation than the default fade out and in then you can do that like so:

<!-- my.component.html -->
<ng2-page-transition [animation]="customAnimation">
    <div [@ng2ElementState]="customAnimation.state">
        Some other content
    </div>
</ng2-page-transition>
 //my.component.ts
 [...]
 import { customTransition } from './custom-transition.animation';

@Component({
  selector: 'my-component', 
  [...]
  animations: [customTransition()],
})
export class MyComponent {
  customAnimation:any = {custom:true, state:""};
}
 //custom-transition.animation.ts
import {trigger, state, animate, style, transition, AnimationEntryMetadata} from '@angular/core';

export function customTransition():AnimationEntryMetadata {
  return slideOutAndIn();
}

function slideOutAndIn():AnimationEntryMetadata {
  return trigger('ng2ElementState', [
    state('leave', style({
        position:'fixed', 
        width:'100%'
    })),
    state('enter', style({
        position:'fixed', 
        width:'100%'
    })),
    transition('* => enter', [
        style({transform: 'translateX(100%)'}),
        animate('0.5s ease-in-out', style({transform: 'translateX(0%)'}))
    ]),
    transition('* => leave', [
      style({transform: 'translateX(0%)'}),
      animate('0.5s ease-in-out', style({transform: 'translateX(-100%)'}))
    ]),
  ]);
}

customAnimation.state has three possible states: "enter", "leave" and "out". "out" is set on the router event NavigationEnd (see https://github.com/bergben/ng2-page-transition/blob/master/src/ng2-page-transition.component.ts#L36)

enterDelay

You can wait for the leaving animation to complete:

 //my.component.ts
 [...]
  customAnimation:any = {custom:true, state:"", enterDelay: 500};

In this case the entering animation would be delayed by 500 ms, allowing the leaving animation which takes 500ms to complete.

To-do

  • Provide a demo

About

Simple Angular2 component to create a page transition animation on route changes

Resources

License

Stars

Watchers

Forks

Packages

No packages published