状态更改时在React JS中实现过渡效果 [英] Implementing transition effects in React JS when state changes

查看:32
本文介绍了状态更改时在React JS中实现过渡效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在React页面上有一张图片.当状态更新为新图像时,我要执行以下过渡效果:

I have an image on a React page. When the state is updated to a new image I want to perform the following transition effect:

  • 原始图像应放大并淡出
  • 新图像也应放大并淡入

效果应类似于穿过墙壁到达新场景.

The effect should look similar to passing through a wall to a new scene.

我如何在React中做到这一点?

How am I able to do this in React?

推荐答案

如@pgsandstrom所述,反应过渡小组是必经之路.不幸的是,这对开发人员不是很友好(学习曲线相当陡峭).

As @pgsandstrom mentioned, React Transition Group is the way to go. Unfortunately, it's not very developer friendly (pretty steep learning curve).

这是一个工作示例: https://codesandbox.io/s/6lmv669kz

✔原始图像在淡出时放大

✔ Original image zooms in while fading out

✔新图像在淡入时放大

TransitionExample.js

import random from "lodash/random";
import React, { Component } from "react";
import { CSSTransition, TransitionGroup } from "react-transition-group";
import uuid from "uuid/v1";

const arr = [
  {
    id: uuid(),
    url: `https://loremflickr.com/600/100?lock=${random(0, 999)}`
  },
  {
    id: uuid(),
    url: `https://loremflickr.com/600/100?lock=${random(0, 999)}`
  },
  {
    id: uuid(),
    url: `https://loremflickr.com/600/100?lock=${random(0, 999)}`
  }
];

export default class TransitionExample extends Component {
  state = {
    index: 0,
    selected: arr[0]
  };

  nextImage = () =>
    this.setState(prevState => {
      const newIndex = prevState.index < arr.length - 1 ? prevState.index + 1 : 0;
      return {
        index: newIndex,
        selected: arr[newIndex]
      };
    });

  render = () => (
    <div className="app">
      <div style={{ marginBottom: 30, height: 100 }}>
        <TransitionGroup>
          <CSSTransition
            key={this.state.selected.id}
            timeout={1000}
            classNames="messageout"
          >
            <div style={{ marginTop: 20 }}>
              <img className="centered-image" src={this.state.selected.url} />
            </div>
          </CSSTransition>
        </TransitionGroup>
      </div>
      <div style={{ textAlign: "center" }}>
        <button
          className="uk-button uk-button-primary"
          onClick={this.nextImage}
        >
          Next Image
        </button>
      </div>
    </div>
  );
}

styles.css

.app {
  margin: 0 auto;
  overflow: hidden;
  width: 700px;
  height: 800px;
}

.centered-image {
  display: block;
  margin: 0 auto;
}

/* starting ENTER animation */
.messageout-enter {
  position: absolute;
  top: 0;
  left: calc(13% + 5px);
  right: calc(13% + 5px);
  opacity: 0.01;
  transform: translateY(0%) scale(0.01);
}

/* ending ENTER animation */
.messageout-enter-active {
  opacity: 1;
  transform: translateY(0%) scale(1);
  transition: all 1000ms ease-in-out;
}

/* starting EXIT animation */
.messageout-exit {
  opacity: 1;
  transform: scale(1.01);
}

/* ending EXIT animation */
.messageout-exit-active {
  opacity: 0;
  transform: scale(4);
  transition: all 1000ms ease-in-out;
}

这篇关于状态更改时在React JS中实现过渡效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆