Angular4通過Directive實(shí)現(xiàn)入場動(dòng)畫

通過Directive實(shí)現(xiàn)入場動(dòng)畫

創(chuàng)建指令并引入所需

import { Directive, ElementRef, HostListener, HostBinding  } from '@angular/core';

@Directive({
  selector: '[VisualIn]'
})
export class VisualInDirective {
  finished: boolean;
  constructor(
    private el: ElementRef,
  ) {
    this.finished = false;
  }

使用HostListener為宿主元素添加window監(jiān)聽

// scroll 監(jiān)聽
@HostListener('window: scroll') elementShowIn() {}
// load 監(jiān)聽
@HostListener('window: load') elementLoadIn() {}

使用HostBinding為宿主元素綁定動(dòng)畫類名

  @HostBinding('class.bounceInRight') isAnimate: boolean;
@keyframes bounceInRight {
  from, 60%, 75%, 90%, to {
    animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
  }

  from {
    opacity: 0;
    transform: translate3d(3000px, 0, 0);
  }

  60% {
    opacity: 1;
    transform: translate3d(-25px, 0, 0);
  }

  75% {
    transform: translate3d(10px, 0, 0);
  }

  90% {
    transform: translate3d(-5px, 0, 0);
  }

  to {
    transform: none;
  }
}

.bounceInRight {
  animation-duration: .5s;
  animation-fill-mode: both;
  animation-name: bounceInRight;
}

獲取元素相對于body標(biāo)簽的高度

documentElement.offsetTop會(huì)獲取標(biāo)簽到offsetParent的相對偏移高度
默認(rèn)的offsetParent返回一個(gè)指向最近的包含該元素的定位元素
如果沒有定位元素則返回最近的table, table sell或根元素
因此我們需要遍歷父級元素,從基元素獲取所有父級定位元素,并累加相對偏移高度

private getOffsetBodyTop(el) {
    let offsetBodyTop = 0;
    function _getOffsetBodyTop(el) {
      offsetBodyTop += el.offsetTop;
      if (el.offsetParent.tagName != 'BODY') {
        return _getOffsetBodyTop(el.offsetParent);
      } else if (el.offsetParent.tagName == 'BODY') {
        return false;
      }
    }
    _getOffsetBodyTop(el);
    return offsetBodyTop;
  }

定位元素進(jìn)入窗口的高度,并執(zhí)行動(dòng)畫

當(dāng) 元素相對根元素的偏移高度 - 當(dāng)前滾動(dòng)高度 - 窗口高度 < 0 時(shí),元素開始進(jìn)入頁面
此時(shí)執(zhí)行動(dòng)畫

private showAnimate(): Promise<any> {
    return new Promise((resolve, reject) => {
    const offsetBodyTop = this.getOffsetBodyTop(this.el.nativeElement); // 元素距離頂部高度
    let _dst: number;   // 滾動(dòng)高度
    if (this.myPhone() === 'iPhone') {
      _dst = document.body.scrollTop;
    } else {
      _dst = document.documentElement.scrollTop;
    }
    const _wsh = window.innerHeight;   // 屏幕高度
    if (offsetBodyTop - _dst - _wsh < -30) {
      if (this.finished) {
        resolve('isFinished');
      } else {
        this.isAnimate = true;
        this.finished = true;
        resolve('show');
      }
    } else {
      resolve('noShow');
    }
  });
}

private myPhone() {
  const userAgent = navigator.userAgent;
  if (userAgent.indexOf('iPhone') > -1) {
    return 'iPhone';
  }

修改HostListener

 @HostListener('window:scroll') elementShowIn() {
    this.showAnimate().then(res => {});
  }
  @HostListener('window:load') elementLoadIn() {
    this.showAnimate().then(res => {});
  }

添加指令

在你需要的元素標(biāo)簽上添加

<div  VisualIn></div>

This is demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,118評論 1 92
  • HTML標(biāo)簽解釋大全 一、HTML標(biāo)記 標(biāo)簽:!DOCTYPE 說明:指定了 HTML 文檔遵循的文檔類型定義(D...
    米塔塔閱讀 3,526評論 1 41
  • 嗯哼嗯哼蹦擦擦~~~ 轉(zhuǎn)載自:https://github.com/Tim9Liu9/TimLiu-iOS 目錄 ...
    philiha閱讀 5,246評論 0 6
  • 1. 我們的一生,會(huì)有四個(gè)重要的關(guān)系:自己與最值得珍惜的人的關(guān)系,即親密關(guān)系;自己與社會(huì)的關(guān)系,比如友誼和事業(yè);自...
    勇敢的心207閱讀 578評論 0 1
  • 作為一個(gè)已經(jīng)大二快奔三的人了,可生活還是千遍一律,都說,靈魂和胃,總有一個(gè)要先在路上,可我還是日復(fù)一日的循環(huán)...
    老干部少爺心閱讀 162評論 1 1

友情鏈接更多精彩內(nèi)容