使用动画时,角度平滑滚动不起作用

时间:2018-08-13 11:24:07

标签: angular smooth-scrolling

我是Angular的新手,并尝试设置 smoothScroll https://github.com/kavil/ng2SmoothScroll

我的视图设置如下:

<piiick-one smoothScroll offset="10" [scrollIf]="step === 1"></piiick-one>
<piiick-two *ngIf="max >= 2" smoothScroll offset="10" [scrollIf]="step === 2"></piiick-two>
<piiick-three *ngIf="max >= 3" smoothScroll offset="10" [scrollIf]="step === 3"></piiick-three>
<piiick-results *ngIf="max >= 4" smoothScroll offset="10" [scrollIf]="step === 4"></piiick-results>

组件的设置如下:

export class StepsComponent implements DoCheck {
  step: number = 0;
  max: number = 0;
  private path: string;

  constructor(
    private route: ActivatedRoute
  ) {
    this.route.params.subscribe(params => this.path = params.path)
  }

  ngDoCheck() {
    switch (this.path) {
      case 'one': this.changeStep(1); break;
      case 'two': this.changeStep(2); break;
      case 'three': this.changeStep(3); break;
      case 'results': this.changeStep(4); break;
      default: this.changeStep(0); break;
    }    
  }

  private changeStep(step) {
    var current = this.step;

    this.step = step;
    if (step > current) {
      this.max = step;
    }
  }
}

应该发生的是,当调用路由/steps/three时,它将 smoothScroll 到ID为 3 的div。 这一直有效,直到我打开页面过渡动画。

我现在在我的app.component中有以下代码:

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

const routerTransition = trigger('routerTransition', [
  transition('* => home', [
    query(':enter, :leave', style({ position: 'fixed', width:'100%' })
      , { optional: true }),
    group([
      query(':enter', [
        style({ transform: 'translateX(-100%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(0%)' }))
      ], { optional: true }),
      query(':leave', [
        style({ transform: 'translateX(0%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(100%)' }))
      ], { optional: true }),
    ])
  ]),
  transition('* => steps', [
    group([
      query(':enter, :leave', style({ position: 'fixed', width:'100%' })
      , { optional: true }),
      query(':enter', [
        style({ transform: 'translateX(100%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(0%)' }))
      ], { optional: true }),
      query(':leave', [
        style({ transform: 'translateX(0%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(-100%)' }))
      ], { optional: true }),
    ])
  ])
])

@Component({
  selector: 'piiick-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  animations: [routerTransition]
})
export class AppComponent {
  prepareRouteTransition(outlet) {
    return outlet.activatedRouteData.state;
  }
}

注释animations时,smoothScroll起作用;但启用后,smoothScroll不会触发。 有人知道为什么吗?

1 个答案:

答案 0 :(得分:0)

我想我找到了答案。 如果找到了一个帖子,其中有人在演示如何在动画完成后触发事件。 因此,我这样创建了一个 AnimationService

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class AnimationService {
  private animatingSubject = new Subject<boolean>();

  animating = this.animatingSubject.asObservable();

  constructor() { }

  loading(state: boolean) {
    this.animatingSubject.next(state);
  }
}

,用于设置动画状态。 我创建了一个 Animations 组件,就像下面这样:

<div [@routerTransition]="prepareRouteTransition(outlet)" (@routerTransition.start)="handleStart()" (@routerTransition.done)="handleDone()">
  <router-outlet #outlet="outlet"></router-outlet>
</div>

,它像这样放在我的 App.Component 中:

<piiick-header></piiick-header>
<piiick-animations></piiick-animations>
<piiick-spinner></piiick-spinner>

组件代码如下:

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

import { AnimationService } from '../../core/services/animation.service';

const routerTransition = trigger('routerTransition', [
  transition('* => home', [
    query(':enter, :leave', style({ position: 'fixed', width:'100%' })
      , { optional: true }),
    group([
      query(':enter', [
        style({ transform: 'translateX(-100%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(0%)' }))
      ], { optional: true }),
      query(':leave', [
        style({ transform: 'translateX(0%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(100%)' }))
      ], { optional: true }),
    ])
  ]),
  transition('* => steps', [
    group([
      query(':enter, :leave', style({ position: 'fixed', width:'100%' })
      , { optional: true }),
      query(':enter', [
        style({ transform: 'translateX(100%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(0%)' }))
      ], { optional: true }),
      query(':leave', [
        style({ transform: 'translateX(0%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(-100%)' }))
      ], { optional: true }),
    ])
  ])
])

@Component({
  selector: 'piiick-animations',
  templateUrl: './animations.component.html',
  styleUrls: ['./animations.component.scss'],
  animations: [routerTransition]
})
export class AnimationsComponent {
  constructor(private animationService: AnimationService) { }

  prepareRouteTransition(outlet) {
    return outlet.activatedRouteData.state;
  }

  handleStart(e) {
    this.animationService.loading(true);
  }

  handleDone(e) {
    this.animationService.loading(false);
  }
}

如您所见,动画开始时,它将更新 AnimationService 并将动画属性设置为true,当动画完成时,它将再次更新服务并将属性设置为false

现在,它只是可以与 smoothScroll 一起使用。 起初,我无法使它起作用,然后我发现,这不仅是我必须等待的动画,而且内容也是如此。因此我在 StepsComponent 中添加了一个属性,该属性在视图初始化后已更新。

我将HTML更新为:

<div *ngIf="scrollIf">
    <piiick-one smoothScroll offset="10" [scrollIf]="step === 1"></piiick-one>
    <piiick-two *ngIf="max >= 2" smoothScroll offset="10" [scrollIf]="step === 2"></piiick-two>
    <piiick-three *ngIf="max >= 3" smoothScroll offset="10" [scrollIf]="step === 3"></piiick-three>
    <piiick-results *ngIf="max >= 4" smoothScroll offset="10" [scrollIf]="step === 4"></piiick-results>

    <piiick-navigation></piiick-navigation>
</div>

,然后将代码更改为此:

export class StepsComponent implements DoCheck, AfterViewInit {
  step: number = 0;
  max: number = 0;
  scrollIf: boolean = false;

  private path: string;
  private afterViewInit: boolean;
  private viewAnimating: boolean;

  constructor(
    private route: ActivatedRoute,
    private animationService: AnimationService
  ) {
    this.route.params.subscribe(params => this.path = params.path);
    this.animationService.animating.subscribe(animating => this.viewAnimating = animating);
  }

  ngAfterViewInit() {
    this.afterViewInit = true;
  }

  ngDoCheck() {
    if (this.afterViewInit) {
      switch (this.path) {
        case 'one': this.changeStep(1); break;
        case 'two': this.changeStep(2); break;
        case 'three': this.changeStep(3); break;
        case 'results': this.changeStep(4); break;
        default: this.changeStep(0); break;
      }
      this.scrollIf = this.afterViewInit && !this.viewAnimating;
    }
  }

  private changeStep(step) {
    var current = this.step;

    this.step = step;
    if (step > current) {
      this.max = step;
    }
  }
}

现在,动画和smoothScroll可以工作了。我所遇到的唯一问题是,视图在初始化之前不会显示。我不确定这是否有问题。