溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在React中寫一個Animation組件并為組件進入和離開加上動畫/過度效果

發(fā)布時間:2021-06-26 13:53:18 來源:億速云 閱讀:235 作者:小新 欄目:web開發(fā)

這篇文章主要為大家展示了“如何在React中寫一個Animation組件并為組件進入和離開加上動畫/過度效果”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何在React中寫一個Animation組件并為組件進入和離開加上動畫/過度效果”這篇文章吧。

問題

在單頁面應用中,我們經(jīng)常需要給路由的切換或者元素的掛載和卸載加上過渡效果,為這么一個小功能引入第三方框架,實在有點小糾結(jié)。不如自己封裝。

思路

原理

以進入時 opacity: 0 --> opacity: 1  ,退出時 opacity: 0 --> opacity: 1 為例

元素掛載時

1.掛載元素dom
2.設置動畫 opacity: 0 --> opacity: 1

元素卸載時

1.設置動畫 opacity: 0 --> opacity: 1
2.動畫結(jié)束后卸載dom

組件設計

為了使得組件簡單易用、低耦合,我們期望如下方式來調(diào)用組件:

屬性名類型描述
isShowBoolean子元素顯示或隱藏控制
nameString指定一個name,動畫進入退出時的動畫

在 App.jsx 里調(diào)用組件:

通過改變isShow的值來指定是否顯示

// App.jsx
// 其他代碼省略
import './app.css';
<Animation isShow={isShow} name='demo'>
  <div class='demo'>
    demo
  </div>
</Animation>
// 通過改變isShow的值來指定是否顯示
在 App.css 里指定進入離開效果:
// 基礎樣式
.demo {
  width: 200px;
  height: 200px;
  background-color: red;
}
// 定義進出入動畫
.demo-showing {
  animation: show 0.5s forwards;
}
.demo-fading {
  animation: fade 0.5s forwards;
}
// 定義動畫fade與show
@keyframes show {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes fade {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

根據(jù)思路寫代碼

// Animation.jsx
import { PureComponent } from 'react';
import './index.css';
class Animation extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      isInnerShow: false,
      animationClass: '',
    };
  }
  componentWillReceiveProps(props) {
    const { isShow } = props;
    if (isShow) {
      // 顯示
      this.show().then(() => {
        this.doShowAnimation();
      });
    } else {
      // 隱藏
      this.doFadeAnimation();
    }
  }
  handleAnimationEnd() {
    const isFading = this.state.animationClass === this.className('fading');
    if (isFading) {
      this.hide();
    }
  }
  show() {
    return new Promise(resolve => {
      this.setState(
        {
          isInnerShow: true,
        },
        () => {
          resolve();
        }
      );
    });
  }
  hide() {
    this.setState({
      isInnerShow: false,
    });
  }
  doShowAnimation() {
    this.setState({
      animationClass: this.className('showing'),
    });
  }
  doFadeAnimation() {
    this.setState({
      animationClass: this.className('fading'),
    });
  }
  /**
   * 獲取className
   * @param {string} inner 'showing' | 'fading'
   */
  className(inner) {
    const { name } = this.props;
    if (!name) throw new Error('animation name must be assigned');
    return `${name}-${inner}`;
  }
  render() {
    let { children } = this.props;
    children = React.Children.only(children);
    const { isInnerShow, animationClass } = this.state;
    const element = {
      ...children,
      props: {
        ...children.props,
        className: `${children.props.className} ${animationClass}`,
        onAnimationEnd: this.handleAnimationEnd.bind(this),
      },
    };
    return isInnerShow && element;
  }
}
export default Animation;

以上是“如何在React中寫一個Animation組件并為組件進入和離開加上動畫/過度效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI