如何在 MUI 中应用自定义动画效果@keyframes?
How to apply custom animation effect @keyframes in MUI?
我在 CSS 中使用 @keyframe
学会了使用动画。但是,我想将我的自定义动画代码写入我的 React 项目(使用 MUI)。我的挑战是如何使用 MUI 中的 makeStyle()
编写 Javascript 代码来自定义我的动画。
我希望能够以百分比自定义转换过程
这次在 MUI 中。我需要能够在 makeStyle()
中编写这样的代码,但我似乎不知道该怎么做。
@keyframes myEffect {
0%{
opacity:0;
transform: translateY(-200%);
}
100% {
opacity:1;
transform: translateY(0);
}
}
这是一个示例,演示了 makeStyles
中的 keyframes
语法:
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
对于那些已经开始使用 Material-UI v5 并且想知道如何使用 Emotion 而不是 makeStyles
来做到这一点的人,下面是一种方法的示例使用 Emotion 的等效样式。
/** @jsxImportSource @emotion/react */
import React from "react";
import ReactDOM from "react-dom";
import { css, keyframes } from "@emotion/react";
import { useTheme } from "@mui/material/styles";
import Button from "@mui/material/Button";
const myEffect = keyframes`
0% {
opacity: 0;
transform: translateY(-200%);
}
100% {
opacity: 1;
transform: translateY(0);
}
`;
const myEffectExit = keyframes`
0% {
opacity: 1;
transform: translateY(0);
}
100% {
opacity: 0;
transform: translateY(-200%);
}
`;
function App() {
const theme = useTheme();
const animatedItem = css`
animation: ${myEffect} 3000ms ${theme.transitions.easing.easeInOut};
`;
const animatedItemExiting = css`
animation: ${myEffectExit} 3000ms ${theme.transitions.easing.easeInOut};
opacity: 0;
transform: translateY(-200%);
`;
const [exit, setExit] = React.useState(false);
return (
<>
<div css={exit ? animatedItemExiting : animatedItem}>
<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);
V5
在v5中,您可以使用keyframes
函数(默认为re-exported from emotion)生成关键帧的样式表:
import { styled } from '@mui/material/styles';
import { keyframes } from '@mui/system';
const spin = keyframes`
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
`;
const RotatedBox = styled("div")({
backgroundColor: "pink",
width: 30,
height: 30,
animation: `${spin} 1s infinite ease`
});
因为 styled
/sx
prop 都在内部使用了情感,你可以将相同的样式对象传递给 sx
prop:
<Box
sx={{
backgroundColor: "pink",
width: 30,
height: 30,
animation: `${spin} 1s infinite ease`
}}
/>
V4
只是 之上的一些注释。如果您使用 makeStyles
定义 keyframe
。请记住在动画名称前加上 $
。我第一次错过了这个小细节,我的代码没有工作,在下面的例子中
const useStyles = makeStyles({
"@keyframes fadeIn": {
"0%": {
opacity: 0,
transform: "translateY(5rem)"
},
"100%": {
opacity: 1,
transform: "translateY(0)"
}
},
selector: {
animation: "$fadeIn .2s ease-in-out"
}
});
而不是
animation: "fadeIn .2s ease-in-out"
应该是
animation: "$fadeIn .2s ease-in-out"
但是如果你在全局范围内定义keyframe
。这里不需要前缀
const useStyles = makeStyles({
"@global": {
"@keyframes fadeIn": {
"0%": {
opacity: 0,
transform: "translateY(5rem)"
},
"100%": {
opacity: 1,
transform: "translateY(0)"
}
}
},
selector: {
animation: "fadeIn .2s ease-in-out" // --> this works
}
});
在 github 上关注此 issue 以获得更多关于此的讨论。
我在 CSS 中使用 @keyframe
学会了使用动画。但是,我想将我的自定义动画代码写入我的 React 项目(使用 MUI)。我的挑战是如何使用 MUI 中的 makeStyle()
编写 Javascript 代码来自定义我的动画。
我希望能够以百分比自定义转换过程
这次在 MUI 中。我需要能够在 makeStyle()
中编写这样的代码,但我似乎不知道该怎么做。
@keyframes myEffect {
0%{
opacity:0;
transform: translateY(-200%);
}
100% {
opacity:1;
transform: translateY(0);
}
}
这是一个示例,演示了 makeStyles
中的 keyframes
语法:
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
对于那些已经开始使用 Material-UI v5 并且想知道如何使用 Emotion 而不是 makeStyles
来做到这一点的人,下面是一种方法的示例使用 Emotion 的等效样式。
/** @jsxImportSource @emotion/react */
import React from "react";
import ReactDOM from "react-dom";
import { css, keyframes } from "@emotion/react";
import { useTheme } from "@mui/material/styles";
import Button from "@mui/material/Button";
const myEffect = keyframes`
0% {
opacity: 0;
transform: translateY(-200%);
}
100% {
opacity: 1;
transform: translateY(0);
}
`;
const myEffectExit = keyframes`
0% {
opacity: 1;
transform: translateY(0);
}
100% {
opacity: 0;
transform: translateY(-200%);
}
`;
function App() {
const theme = useTheme();
const animatedItem = css`
animation: ${myEffect} 3000ms ${theme.transitions.easing.easeInOut};
`;
const animatedItemExiting = css`
animation: ${myEffectExit} 3000ms ${theme.transitions.easing.easeInOut};
opacity: 0;
transform: translateY(-200%);
`;
const [exit, setExit] = React.useState(false);
return (
<>
<div css={exit ? animatedItemExiting : animatedItem}>
<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);
V5
在v5中,您可以使用keyframes
函数(默认为re-exported from emotion)生成关键帧的样式表:
import { styled } from '@mui/material/styles';
import { keyframes } from '@mui/system';
const spin = keyframes`
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
`;
const RotatedBox = styled("div")({
backgroundColor: "pink",
width: 30,
height: 30,
animation: `${spin} 1s infinite ease`
});
因为 styled
/sx
prop 都在内部使用了情感,你可以将相同的样式对象传递给 sx
prop:
<Box
sx={{
backgroundColor: "pink",
width: 30,
height: 30,
animation: `${spin} 1s infinite ease`
}}
/>
V4
只是 makeStyles
定义 keyframe
。请记住在动画名称前加上 $
。我第一次错过了这个小细节,我的代码没有工作,在下面的例子中
const useStyles = makeStyles({
"@keyframes fadeIn": {
"0%": {
opacity: 0,
transform: "translateY(5rem)"
},
"100%": {
opacity: 1,
transform: "translateY(0)"
}
},
selector: {
animation: "$fadeIn .2s ease-in-out"
}
});
而不是
animation: "fadeIn .2s ease-in-out"
应该是
animation: "$fadeIn .2s ease-in-out"
但是如果你在全局范围内定义keyframe
。这里不需要前缀
const useStyles = makeStyles({
"@global": {
"@keyframes fadeIn": {
"0%": {
opacity: 0,
transform: "translateY(5rem)"
},
"100%": {
opacity: 1,
transform: "translateY(0)"
}
}
},
selector: {
animation: "fadeIn .2s ease-in-out" // --> this works
}
});
在 github 上关注此 issue 以获得更多关于此的讨论。