2016-06-19 10 views
6

私は2つのルーティングされたコンポーネントと2つのルーターリンクが固定ナビゲーションバーにあるとしましょう。私は、ルータリンクをクリックすると、それらを右からスライドさせたいと思う。ルーティングされたコンポーネントの角2 "スライド"

コンポーネントをcssでオフセットし、タイムアウト関数を使用してcssクラスを(ngStyleまたはngClassなどで)スライドインするように変更したくないです。

これ以上のエレガントな方法は、角度2で達成することができますか?

ありがとうございます!

+0

RC2の新しいアニメーションのAPIがあります、あなたはそれに見ることができます。 [この例を参照](https://angular.io/docs/ts/latest/guide/animations.html#!#example-entering-and-leaving)私はまだルータで試してみませんでしたが、funのように見えますが(: – Sasxa

+0

)答えは主にTypeScriptの埋め込みCSSでアニメーションを表示します。/outエフェクト? – Mark

答えて

7

特定のルートアニメーションを作成できるようになりました。これは、コンポーネントが表示されているときにアニメーションをトリガーするのとは異なり、スムーズなトランジションのために入力/出力コンポーネントを同時にアニメートさせることができ、どのコンポーネントが出たり来たりしているかに応じてトランジションを変更できます。つまり、コンテンツをドリルダウンする場合は右からコンポーネントをスライドし、別のコンポーネントの「戻る」ボタンを使用して入力する場合は左からスライドさせるなど、複雑なトランジションを行うことができます。

  1. まず、(例えばapp.component.html。)ので、同様にルーターのコンセントに注釈を付ける:

    <div class="page" [@routerAnimations]="prepareRouteTransition(outlet)"> 
        <router-outlet #outlet="outlet"></router-outlet> 
    </div> 
    
  2. 対応するコンポーネント定義(例えばapp.component.js)でprepareRouteTransition(outlet)機能を実装します。

    prepareRouteTransition(outlet) { 
        const animation = outlet.activatedRouteData['animation'] || {}; 
        return animation['value'] || null; 
    } 
    
  3. (例えばapp.component.jsを)あなたのアニメーションを定義します "を登録し、最後に

    const routes: Routes = [ 
        { 
        path: 'products', 
        component: ProductsComponent, 
        data: { 
         animation: { 
         value: 'products', 
         } 
        } 
        }, 
        { 
        path: 'products/:id', 
        component: ProductDetailComponent, 
        data: { 
         animation: { 
         value: 'product-detail', 
         } 
        } 
        } 
    
  4. const slideLeft = [ 
        query(':leave', style({ position: 'absolute', left: 0, right: 0 ,transform: 'translate3d(0%,0,0)' }), {optional:true}), 
        query(':enter', style({ position: 'absolute', left: 0, right: 0, transform: 'translate3d(-100%,0,0)' }), {optional:true}), 
        group([ 
         query(':leave', group([ 
         animate('500ms cubic-bezier(.35,0,.25,1)', style({ transform: 'translate3d(100%,0,0)' })), // y: '-100%' 
         ]), {optional:true}), 
         query(':enter', group([ 
         animate('500ms cubic-bezier(.35,0,.25,1)', style({ transform: 'translate3d(0%,0,0)' })), 
         ]), {optional:true}) 
        ]) 
        ] 
    
        const slideRight = [ 
        query(':leave', style({ position: 'absolute', left: 0, right: 0 , transform: 'translate3d(0%,0,0)'}), {optional:true}), 
        query(':enter', style({ position: 'absolute', left: 0, right: 0, transform: 'translate3d(100%,0,0)'}), {optional:true}), 
    
        group([ 
         query(':leave', group([ 
         animate('500ms cubic-bezier(.35,0,.25,1)', style({ transform: 'translate3d(-100%,0,0)' })), // y: '-100%' 
         ]), {optional:true}), 
         query(':enter', group([ 
         animate('500ms cubic-bezier(.35,0,.25,1)', style({ transform: 'translate3d(0%,0,0)' })), 
         ]), {optional:true}) 
        ]) 
        ] 
    
  5. は、あなたのルートの定義(例えばapp.routing.ts)にアニメーションメタデータを追加します。あなたが定義したアニメーションとルートメタデータを使用してコンポーネント上のrouterAnimationsのアニメーショントリガーapp.component.js):

    @Component({ 
        selector: 'app-root', 
        templateUrl: './app.component.html', 
        styleUrls: ['./app.component.css'], 
        animations: [ 
        trigger('routerAnimations', [ 
         transition('products => product-detail', slideRight), 
         transition('product-detail => products', slideLeft), 
        ]) 
        ] 
    }) 
    

古いブラウザを対象とするウェブアニメーションAPIをポリフィルすることを忘れないでください

マティアスNiemelaは()のデモで、ここでNG-confにルートアニメーションについての詳細を語る:https://youtu.be/Oh9wj-1p2BM?t=12m21s

彼のプレゼンテーションコード:https://github.com/matsko/ng4-animations-preview

+0

これを実行した後にAngularプロジェクトを構築できないようです。 githubプロジェクトは、まったく新しいものを構築できないようです –

+0

ええと、2つの異なるノートパソコンのgithubプロジェクトをクローンしてビルドして実行することができました。 – SpaceFozzy

+0

ご回答いただきありがとうございます。これは、最新のCLIを使用しているコンピュータで起こることです。 'ng build --aot --prod'を実行すると&node。 http://i.imgur.com/kp8mrVo.jpg –

7

滑りの点では、それはかなり簡単です。

Official Angular 2 Animate docsを参照できます。

また、新しいルータv3では、私が実際に休暇/終了/無効遷移持ってする方法を見つけ出すのに苦労しています念頭に

ベアを使用して、私はシンプルなショーケースのためにしたこのPlunkerをチェックアウトすることができますトリガされた要素がビューから破棄されようとしています。

Angular 2 Animate - No visible effect of the '* => void' transition when changing routes/componentsに別のスレッドを開いて、ルータに残っているアニメーション/遷移時間を通知する方法を見つけようとしました。アンギュラ4.1

@Component({ 
    selector: 'home', 
    directives: [ROUTER_DIRECTIVES], 
    template: ` 
    <div @flyInOut="'active'" class="radibre"> 
    </div> 
    `, 
    styles: ['.radibre { width: 200px; height: 100px; background: red; }'], 
    animations: [ 
    trigger('flyInOut', [ 
     state('in', style({transform: 'translateX(0)'})), 
     transition('void => *', [ 
     style({transform: 'translateX(-100%)'}), 
     animate(100) 
     ]), 
     transition('* => void', [ 
     animate(100, style({transform: 'translateX(100%)'})) 
     ]) 
    ]) 
    ] 
}) 

export class Home { 
    constructor() { } 
} 
@Component({ 
    selector: 'page', 
    template: ` 
    <div @testingBottom="'active'" class="page"></div>`, 
    styles: ['.page { width: 300px; height: 50px; background: green; }'], 
    animations: [ 
    trigger('testingBottom', [ 
     state('active', style({transform: 'scale(1)'})), 
     transition('void => *', [ 
     style({transform: 'scale(0)'}), 
     animate(100) 
     ]), 
     transition('* => void', [ 
     animate(100, style({transform: 'scale(0)'})) 
     ]) 
    ]) 
    ] 
}) 
+7

Plunkerがトランジションをアニメーション化しないことに注意してください(おそらく、* rc_ *から* stable *へのangular2のアップデートのため) – msanford

関連する問題