如何使用makestyles()在MaterialUI中应用自定义动画效果@keyframes [英] How to apply custom animation effect @keyframes in MaterialUI using makestyles()

查看:213
本文介绍了如何使用makestyles()在MaterialUI中应用自定义动画效果@keyframes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学会了使用@keyframe在CSS中使用动画.但是我想将我的自定义动画代码编写到我的react项目中(使用materialUI).我面临的挑战是如何在MaterialUI中使用makeStyle()编写javascript代码以自定义动画. 我希望能够以百分比自定义过渡过程 这次在materialUI中.我需要能够在makeStyle()中编写这样的代码,但我似乎不知道该怎么做.

I have learnt to use animation in css using @keyframe. I however want to write my custom animation code to my react project(Using materialUI). My challenge is how I can write the javascript code to custom my animations using the makeStyle() in MaterialUI. I want to be able to custom the transitions processes in percentages this time around in materialUI. I need to be able to write codes like this in makeStyle() but I don't seem to know how to.

@keyframes myEffect {
 0%{
  opacity:0;
  transform: translateY(-200%); 
 }

100% {
  opacity:1;
  transform: translateY(0);
 }
}

推荐答案

下面是一个示例,演示了makeStyles中的keyframes语法:

Here is an example demonstrating the keyframes syntax within makeStyles:

import React from "react";
import ReactDOM from "react-dom";

import { makeStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";
import clsx from "clsx";

const useStyles = makeStyles(theme => ({
  animatedItem: {
    animation: `$myEffect 3000ms ${theme.transitions.easing.easeInOut}`
  },
  animatedItemExiting: {
    animation: `$myEffectExit 3000ms ${theme.transitions.easing.easeInOut}`,
    opacity: 0,
    transform: "translateY(-200%)"
  },
  "@keyframes myEffect": {
    "0%": {
      opacity: 0,
      transform: "translateY(-200%)"
    },
    "100%": {
      opacity: 1,
      transform: "translateY(0)"
    }
  },
  "@keyframes myEffectExit": {
    "0%": {
      opacity: 1,
      transform: "translateY(0)"
    },
    "100%": {
      opacity: 0,
      transform: "translateY(-200%)"
    }
  }
}));

function App() {
  const classes = useStyles();
  const [exit, setExit] = React.useState(false);
  return (
    <>
      <div
        className={clsx(classes.animatedItem, {
          [classes.animatedItemExiting]: exit
        })}
      >
        <h1>Hello CodeSandbox</h1>
        <h2>Start editing to see some magic happen!</h2>
        <Button onClick={() => setExit(true)}>Click to exit</Button>
      </div>
      {exit && <Button onClick={() => setExit(false)}>Click to enter</Button>}
    </>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

文档: https://cssinjs.org/jss-syntax/?v=v10.0.0#keyframes-animation

这篇关于如何使用makestyles()在MaterialUI中应用自定义动画效果@keyframes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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