问题

在单页面应用中,我们经常需要给路由的切换或者元素的挂载和卸载加上过渡效果,为这么一个小功能引入第三方框架,实在有点小纠结。不如自己封装。

思路

原理

以进入时opacity: 0 --> opacity: 1 ,退出时opacity: 0 --> opacity: 1为例

元素挂载时

  1. 挂载元素dom
  2. 设置动画opacity: 0 --> opacity: 1

元素卸载时

  1. 设置动画opacity: 0 --> opacity: 1
  2. 动画结束后卸载dom

组件设计

为了使得组件简单易用、低耦合,我们期望如下方式来调用组件:

属性名 类型 描述
isShow Boolean 子元素显示或隐藏控制
name String 指定一个name,动画进入退出时的动画

App.jsx里调用组件:

通过改变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;
}
}

根据思路写代码

// 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;

Demo示例

点我直达

在React中写一个Animation组件,为组件进入和离开加上动画/过度的更多相关文章

  1. 放弃antd table,基于React手写一个虚拟滚动的表格

    缘起 标题有点夸张,并不是完全放弃antd-table,毕竟在react的生态圈里,对国人来说,比较好用的PC端组件库,也就antd了.即便经历了2018年圣诞彩蛋事件,antd的使用者也不仅不减,反 ...

  2. 手写一个简单的starter组件

    spring-boot中有很多第三方包,都封装成starter组件,在maven中引用后,启动springBoot项目时会自动装配到spring ioc容器中. 思考: 为什么我们springBoot ...

  3. 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能

    在web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面,而重定向到登陆界面的功能. 项目是用springMVC+spring+hibernate实现 (和这个没有多大关系) 第一步: 首先 ...

  4. Ionic 2 中创建一个照片倾斜浏览组件

    内容简介 今天介绍一个新的UI元素,就是当我们改变设备的方向时,我们可以看到照片的不同部分,有一种身临其境的感觉,类似于360全景视图在移动设备上的应用. 倾斜照片浏览 Ionic 2 实例开发 新增 ...

  5. Tsx写一个通用的button组件

    一年又要到年底了,vue3.0都已经出来了,我们也不能一直还停留在过去的js中,是时候学习并且在项目中使用一下Ts了. 如果说jsx是基于js的话,那么tsx就是基于typescript的 废话也不多 ...

  6. AngularJS中写一个包裹HTML元素的directive

    有这样的一个场景,这里有一个表单: <form role="form">    ...</form> 我们希望在form的外层动态包裹上一层. 有可能是这样 ...

  7. 在vue中写一个跟着鼠标跑的div,div里面动态显示数据

    1.div应该放在body里面,这是我放在body中的一个div里面的div <!-- 信息查看层 --> <div class="floatDiv" :styl ...

  8. 如何在react中实现一个倒计时组件

    倒计时组件 import React, { Component } from 'react' import $ from 'jquery' import "../../css/spellTE ...

  9. 如何在 React Native 中写一个自定义模块

    https://my.oschina.net/jpushtech/blog/983230

随机推荐

  1. 不知道多大的文件不要用cat查看!

    今天长清女子学校的主任给我微信上发了一图片,说登录服务器的时候就查看了一个文件,结果服务器的风扇忽然变的特别响,系统慢了好多,让我看看是什么回事!我当时心里想,无缘无故怎么会这样,难不成是进病毒了?查 ...

  2. zz1998_Efficient Backprop笔记

    1998_Efficient Backprop笔记 A few practical tricks 1. Stochastic vs Batch learning 在最小值附近震荡的幅度与学习速率成比例 ...

  3. 11/5 <backtracking> 伪BFS+回溯

    78. Subsets While iterating through all numbers, for each new number, we can either pick it or not p ...

  4. 【2019.7.25 NOIP模拟赛 T1】变换(change)(思维+大分类讨论)

    几个性质 我们通过推式子可以发现: \[B⇒AC⇒AAB⇒AAAC⇒C\] \[C⇒AB⇒AAC⇒AAAB⇒B\] 也就是说: 性质一: \(B,C\)可以相互转换. 则我们再次推式子可以发现: \[ ...

  5. [LeetCode] 417. Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  6. shell编程题(七)

    输出本机创建20000个目录所用的时间 #! /bin/bash time ( ..} ; do mkdir /tmp/nnn$i done ) 运行记得删除 rm -rf /tmp/nnn*

  7. Office 2016正式版/2019预览版 使用注意

    改写状态已经被隐藏 右击Word状态栏可以看到改写是否打开

  8. 获取oracle中某张表的各个字段名称

    select column_name from all_tab_columns where table_name='AT2_SSIS_CHNDB_CLIENT_INFO'

  9. 分布式数据库缓存系统Apache Ignite

    Apache Ignite内存数据组织是高性能的.集成化的以及分布式的内存平台,他可以实时地在大数据集中执行事务和计算,和传统的基于磁盘或者闪存的技术相比,性能有数量级的提升. 将数据存储在缓存中能够 ...

  10. EventBus原理解析

    前言 EventBus的核心思想是观察者模式 (生产/消费者编程模型) . SpringBoot+EventBus使用教程(一) SpringBoot+EventBus使用教程(二) 通过前面的文章我 ...