鍍金池/ 問答/HTML5  HTML/ angular5的動效(animations)怎么實現(xiàn)類似jq的stop()效果

angular5的動效(animations)怎么實現(xiàn)類似jq的stop()效果

就是動畫沒有done之前不再執(zhí)行start,animateleave時延遲的話,延遲沒有結束不再執(zhí)行start。按照官網(wǎng)的例子寫的一個dialog組件,鼠標指針hover時候顯隱。

但是快速劃過的時候出現(xiàn)的樣子不太友好,求問如何解決。

圖片描述

drop-dialog.component.ts

...
@Component({
  selector: 'app-drop-dialog',
  templateUrl: './drop-dialog.component.html',
  styleUrls: ['./drop-dialog.component.scss'],
  animations: [
    trigger('fade', [
      state(ANIMATE_STATES[0], style({
        opacity: 1,
        transform: 'scale(1)'
      })),
      transition(`void => *`, [
        animate('300ms ease-in', keyframes([
          style({ opacity: 0, transform: 'scale(0)', offset: 0 }),
          style({ opacity: 1, transform: 'scale(1.1)', offset: 0.6 }),
          style({ opacity: 1, transform: 'scale(1)', offset: 1 })
        ]))
      ]),
      transition('* => void', [
        animate('200ms 400ms ease-out', keyframes([
          style({ opacity: 1, transform: 'scale(1)', offset: 0 }),
          style({ opacity: 0, transform: 'scale(0)', offset: 1 })
        ]))
      ])
    ])
  ]
})
export class DropDialogComponent implements OnInit {
  public state: string;
  ...

  @Input() show: boolean;
  ...
  ngOnInit() {
    ...

    this.state = this.show ? ANIMATE_STATES[0] : ANIMATE_STATES[1];
  }
  ...
}

drop-dialog.component.html

<div class="drop-dialog"
     [ngClass]="direction"
     [ngStyle]="dialogStyle"
     [@fade]="state"
     (@fade.start)="animateStart()"
     (@fade.done)="animateDone()"
     *ngIf="show">
  <i class="arrow" [ngStyle]="arrowStyle"></i>
  <div class="content">
    <ng-content></ng-content>
  </div>
</div>

@Input() show是從組件外傳入的bool值。

parent.component.ts

<li class="pull-right setting" (mouseover)="toggleSetting(1)"
  (mouseleave)="toggleSetting(0)">
<a href="javascript: void(0);" class="icon">
  <img src="../../../../assets/img/shared/setting.png" alt="">

  <app-drop-dialog
    direction="bot"
    arrow="17px"
    [position]="['0px', '70px']"
    [size]="['200px', '254px']"
    [show]="settingDialog">
    <div class="wrapper setting-dialog">
      <app-switch [init]="false" (change)="change($event)"></app-switch> <span>{{ isSelect }}</span>
    </div>
  </app-drop-dialog>
</a>
</li>
回答
編輯回答
薔薇花
export class HeaderComponent implements OnInit {
    private timer = 0;
    public settingDialog = false;
    
    ...
    toggleSetting(isShow: any) {
        const show = !!isShow;
        clearTimeout(this.timer);
        if (!show) {
            // delay 600ms for hide
            this.timer = setTimeout(() => {
                this.settingDialog = show;
            }, 600);
            return;
        }
        this.settingDialog = show;
    }
    ...
}
2017年2月13日 09:43