html - Angular Animations 滑入不起作用,只是出现

标签 html css angular angular-animations

我正在尝试使用 Angular Animations 实现路线转换、滑入。

过渡类型的作品 - 它不会滑入,而是滑出。滑入感觉就像一个滞后,它只是出现。为何如此?

export const slider =
  trigger('routeAnimations', [
    transition('* => isLeft', slideTo('left')),
    transition('* => isRight', slideTo('right')),
    transition('isRight => *', slideTo('left')),
    transition('isLeft => *', slideTo('right'))
  ]);

function slideTo(direction) {
  const optional = {optional: true};
  return [
    query(':enter, :leave', [
      style({
        position: 'absolute',
        top: 0,
        [direction]: 0,
        width: '100%'
      })
    ], optional),
    query(':enter', [
      style({[direction]: '-100%'})
    ]),
    group([
      query(':leave', [
        animate('600ms ease', style({[direction]: '100%'}))
      ], optional),
      query(':enter', [
        animate('600ms ease', style({[direction]: '0%'}))
      ])
    ]),
  ];
}


Route:
const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'project/:title', component: ProjectComponent },
  { path: 'about', component: AboutComponent, data: { animation: 'isRight' } }
];

最佳答案

这是我创建的滑入/滑出路由器动画:

// router-animation.ts

import { trigger, transition, style, query, group, animateChild, animate, keyframes, } from '@angular/animations';

export const slider =
trigger('routeAnimations', [
    transition('isRight => isLeft', slideTo('left') ),
    transition('isLeft => isRight', slideTo('right') ),
    // transition('isRight => *', slideTo('left') ),
    // transition('isLeft => *', slideTo('right') )
]);

function slideTo(direction) {
const optional = { optional: true };
return [
    query(':enter, :leave', [
    style({
        position: 'absolute',
        top: 0,
        [direction]: 0,
        width: '100%'
    })
    ], optional),
    query(':enter', [
    style({ [direction]: '-110%'})
    ]),
    group([
    query(':leave', [
        animate('600ms ease', style({ [direction]: '110%'}))
    ], optional),
    query(':enter', [
        animate('600ms ease', style({ [direction]: '0%'}))
    ])
    ]),
    // Normalize the page style... Might not be necessary

    // Required only if you have child animations on the page
    // query(':leave', animateChild()),
    // query(':enter', animateChild()),
];
}

在路由模块上:

// app-routing.module.ts

const routes: Routes = [
    { path: '', redirectTo: 'route-1', pathMatch: 'full' },
    { path: 'route-1', component: Route1Component, data: { animation: 'isLeft'} },
    { path: 'route-2', component: Route2Component, data: { animation: 'isRight'} },
    { path: 'route-3', component: Route3Component }
];

app.component.html 文件中:

<!-- app.component.html -->

<div [@routeAnimations]="prepareRoute(outlet)">
    <router-outlet #outlet="outlet"></router-outlet>
</div>

最后在 app.component.ts 文件中:

// app.component.ts

prepareRoute(outlet: RouterOutlet) {
    return outlet && outlet.activatedRouteData && outlet.activatedRouteData['animation'];
}

您可以在 StackBlitz here 中查看.

关于html - Angular Animations 滑入不起作用,只是出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59315569/

相关文章:

angular - 如何在angular2中使用组件名称动态加载组件?

html - 无序列表中的无序列表 - 选择特定的行元素

html - 如何为 HtmlAgilityPack HtmlDocument 设置编码

javascript - 引导轮播失败

javascript - fabricjs中对象的属性是什么类似于css的溢出: hidden

jquery - 修复不同高度的 Zurb Foundation Grid

javascript - Typescript 包装类型

html - 何时使用 CSS 何时使用 HTML

CSS:图像和文本在一行的右边,没有流文本?

angular - 如何在库 angular 中使用 scss 文件创建组件