12 react 基础 的 css 过渡动画 及 动画效果 及 使用 react-transition-group 实现动画
一. 过渡动画
# index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
ReactDOM.render(<App />, document.getElementById('root'));
# app.js
import React, { Component, Fragment } from 'react';
import './style.css';
class app extends Component{
constructor(props){
super(props);
this.state = {
show : true
};
this.DivToggle = this.DivToggle.bind(this);
}
DivToggle(){
console.log(this.state.show)
this.setState({
show: this.state.show ? false : true
})
}
render() {
return (
<Fragment>
<div className={this.state.show? 'show': 'hide'}>你好啊</div>
<button onClick={this.DivToggle}>toggle</button>
</Fragment>
)
}
}
export default app;
# style.css
.show{
opacity: 1;
transition: all 1s ease-in;
}
.hide{
opacity: 0;
transition: all 1s ease-in;
}
二.动画效果
使用 keyframes 进行渲染 动画
# style.css
.show{
/* 使用 forwards css 动画最后一帧 保存 样式*/
animation: show-item 2s ease-in forwards;
}
.hide{
/* 使用 forwards css 动画最后一帧 保存 样式*/
animation: hide-item 2s ease-in forwards;
}
@keyframes hide-item {
0% {
opacity: 1;
color: red;
}
50% {
opacity: 0.5;
color: green;
}
100% {
opacity: 0;
color: blue;
}
}
@keyframes show-item {
0% {
opacity: 0;
color: blue;
}
50% {
opacity: 0.5;
color: green;
}
100% {
opacity: 1;
color: red;
}
}
三. 使用 react-transition-group CSSTransition 实现动画 (github 地址) (文档)
1. 安装 react-transition-group
yarn add react-transition-group
2. 引入 css-transition
import { CSSTransition } fron 'react-transition-group'
3. 将要 改变样式的元素用 CSSTransition 标签包裹起来
eg:
# index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
ReactDOM.render(<App />, document.getElementById('root'));
# app.js
import React, {Component, Fragment} from 'react';
import {CSSTransition} from 'react-transition-group'
import './style.css'
class app extends Component
{
constructor(props){
super(props);
this.state={
show : true
}
this.DivToggle = this.DivToggle.bind(this);
}
DivToggle(){
console.log(this.state.show);
this.setState({
show: this.state.show? false : true
});
}
render(){
return (
<Fragment>
<CSSTransition
in={this.state.show}
timeout={1000}
classNames = "fade"
// 当隐藏后 去除挂载
unmountOnExit
// 当显示完成时 颜色变蓝 el 指 CSSTransition 内包裹的元素
onEntered={(el)=>{ el.style.color='red' }}
// 当退出显示时 颜色变黑 el 指 CSSTransition 内包裹的元素
onExited={(el)=>{ el.style.color='black' }}
// 第一次入场动画也要引入动画
appear={true}
>
<div className={this.state.show?'show':'hide'}>hello</div>
</CSSTransition>
<button type="button" onClick={this.DivToggle}>点一下</button>
</Fragment>
);
}
}
export default app;
#style.css
四. 使用react-transition-group CSSTransition TransitionGroup 实现多个元素样式动画
#index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
ReactDOM.render(<App />, document.getElementById('root'));
#app.js
import React, {Component, Fragment} from 'react';
import {CSSTransition, TransitionGroup} from 'react-transition-group'
import './style.css'
class app extends Component
{
constructor(props){
super(props);
this.state={
list : []
}
this.AddItem = this.AddItem.bind(this);
}
AddItem (){
this.setState((prevState)=>{
return {
list : [...prevState.list, 'item']
}
})
}
render(){
return (
<Fragment>
{/* TransitionGroup 包裹住要展示动画的元素 */}
<TransitionGroup>
{
this.state.list.map((val, index)=>{
return (
// 内部要展示样式的元素 使用 CSSTransition 包裹并边写动画效果
// 这时可以删除 in 效果 没有手动点击变换效果
<CSSTransition
timeout={1000}
classNames="fade"
onEntered={(el)=>{el.style.color="red"}}
appear={true}
key={index}
>
<div key={index} >{val}</div>
</CSSTransition>
)
})
}
</TransitionGroup>
<button type="button" onClick={this.AddItem}>点一下</button>
</Fragment>
);
}
}
export default app;
#style.css
12 react 基础 的 css 过渡动画 及 动画效果 及 使用 react-transition-group 实现动画的更多相关文章
- Unity3D中暂停时的动画及粒子效果实现
暂停是游戏中经常出现的功能,而Unity3D中对于暂停的处理并不是很理想.一般的做法是将Time.timeScale设置为0.Unity的文档中对于这种情况有以下描述: The scale at wh ...
- react中使用css动画效果
index.js import React, { Component, Fragment } from 'react'; class App extends Component { construct ...
- CSS过渡动画之transition
O(∩_∩)O~ 这两天在看看CSS的相关内容,关于transition动画感觉很有意思,分享一下. CSS负责给html加效果,自然少不了各种动画,今天介绍一下transition. 概述 看一段比 ...
- Vue——关于css过渡和动画那些事
1. 单元素/组件的过渡transition Vue 提供了 transition 的封装组件,在下列情形中,可以给任何元素和组件添加进入/离开过渡 条件渲染 (使用 v-if) 条件展示 (使用 v ...
- 36.React基础介绍——2019年12月24日
2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...
- CSS过渡、CSS动画
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <script s ...
- 前端总结·基础篇·CSS(二)视觉
前端总结系列 前端总结·基础篇·CSS(一)布局 前端总结·基础篇·CSS(二)视觉 前端总结·基础篇·CSS(三)补充 前端总结·基础篇·CSS(四)兼容 目录 一.动画(animation)(IE ...
- 第213天:12个HTML和CSS必须知道的重点难点问题
12个HTML和CSS必须知道的重点难点问题 这12个问题,基本上就是HTML和CSS基础中的重点个难点了,也是必须要弄清楚的基本问题,其中定位的绝对定位和相对定位到底相对什么定位?这个还是容易被忽视 ...
- 前端总结·基础篇·CSS
前端总结·基础篇·CSS 1 常用重置+重置插件(Normalize.css,IE8+) * {box-sizing:border-box;} /* IE8+ */body {margin:0;} ...
随机推荐
- Vue生命周期 钩子函数和组件传值
Vue生命周期 钩子函数 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听.编译模板.将实例挂载到 DOM 并在数据变化时更新 DOM 等. 同时在这个过程中也会运行一 ...
- Android实现三级联动下拉框下拉列表spinner
原文出处:http://www.cnblogs.com/zjjne/archive/2013/10/03/3350107.html 主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下 ...
- 008、Java中变量与常量的区别
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- eshop1-大型电商架构演进
1. 项目初期 2. 服务器分离 以上的服务分离架构,即使文件服务crash 了,但是application server 和 Database Server 继续可以访问运行 3. 基于并发访问越来 ...
- HTML 5 <blockquote><p>的分工与合作
一提到文档标签,大家首先想到的就是p,那如果要实现缩进及间距,还得使用margin,padding及text-indent等css样式. 但现在html5的一个新标签解决了以上所有问题,它可以自缩进和 ...
- HDU - 3729 I'm Telling the Truth(二分匹配)
题意:有n个人,每个人给出自己的名次区间,问最多有多少个人没撒谎,如果有多解,输出字典序最大的解. 分析: 1.因为字典序最大,所以从后往前分析. 2.假设后面的人没说谎,并将此作为已知条件,然后从后 ...
- Idea 打开多profile注意事项
Maven项目经常会有多个profile,可以方便在编译时指定profile. 如果有多个profile,idea 在打开工程后默认配置可能会有些问题. 例如: 最近在编译一个项目:https://g ...
- CentOS 6.x 重置root 密码
1.重启,进入启动界面,快速按e,进入GNU GRUB界面. 2.选择第二项,按e,进行编辑. 3.在末尾输入1或single,回车,返回上一界面,还是选第二项,按b,进入单用户模式. 此时输入命令 ...
- 小程序跳坑之JSON字符串转换JSON对象
常见的JSON字符串转换有很多,这里只讲我遇到过的小程序中用到的转换. 通常我们在小程序中用到的地方是,请求一个数据表或者请求一个接口,拿到了一堆数据,里面包含有各种字段数组,头像,图片,详情,地址, ...
- tomcat-jvm内存问题
http://www.360doc.com/content/14/0617/12/114824_387440563.shtml http://27091497.blog.163.com/blog/st ...