在 Framer Motion 中不起作用时的过渡
Transition when doesn't work in Framer Motion
我正在使用 Framer Motion as an animation library in React project. I am trying to animate parent element after child element using when 属性。它不起作用,因为 ContentVariants
和 ImgVariants
同时是 运行。
import React, { Component } from "react";
import ReactDOM from "react-dom";
import styled from "styled-components";
import { motion } from "framer-motion";
export const ContentVariants = {
expanded: () => ({
width: "150px",
transition: {
when: "afterChildren",
duration: 2
}
}),
collapsed: () => ({
width: "50px",
transition: {
when: "afterChildren",
duration: 2
}
})
};
export const Content = styled(motion.div)`
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
background-color: burlywood;
padding: 30px;
height: 500px;
`;
export const ToggleBtn = styled.button`
padding: 5px 10px;
cursor: pointer;
display: flex;
width: auto;
align-self: flex-end;
`;
export const ImgVariants = {
expanded: {
width: "100px",
scale: 1,
transition: {
duration: 2
}
},
collapsed: {
scale: 0.5,
transition: {
duration: 2
}
}
};
const Img = styled(motion.img)``;
class App extends Component {
state = {
collapsed: false
};
toggle = () => {
this.setState({ collapsed: !this.state.collapsed });
};
render() {
const { collapsed } = this.state;
return (
<div>
<Content
initial={collapsed ? "collapsed" : "expanded"}
animate={collapsed ? "collapsed" : "expanded"}
variants={ContentVariants}
>
<Img
src="https://picsum.photos/200/200"
initial={collapsed ? "collapsed" : "expanded"}
animate={collapsed ? "collapsed" : "expanded"}
variants={ImgVariants}
/>
<ToggleBtn onClick={this.toggle}>toggle</ToggleBtn>
</Content>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
如果我在 ContentVariants
中将 when: "afterChildren"
更改为 when: "beforeChildren"
,它没有任何区别。即使我删除 when
属性,动画也会同时 运行。
文档传播部分 (https://www.framer.com/api/motion/animation/#propagation) 说
If a motion component has children, changes in variant will flow down
through the component hierarchy. These changes in variant will flow
down until a child component defines its own animate property.
您必须从 Img
个元素中删除 animate
属性。
如果您在 child 上设置动画,您的 parent 将不会将其动画逻辑传递给它。因此,您必须从 <Img>
组件中删除 initial
和 animate
属性:
<Img
src="https://picsum.photos/200/200"
variants={ImgVariants}
/>
您可以从官方文档中查看此示例以供参考:https://www.framer.com/api/motion/types/#orchestration.when
我正在使用 Framer Motion as an animation library in React project. I am trying to animate parent element after child element using when 属性。它不起作用,因为 ContentVariants
和 ImgVariants
同时是 运行。
import React, { Component } from "react";
import ReactDOM from "react-dom";
import styled from "styled-components";
import { motion } from "framer-motion";
export const ContentVariants = {
expanded: () => ({
width: "150px",
transition: {
when: "afterChildren",
duration: 2
}
}),
collapsed: () => ({
width: "50px",
transition: {
when: "afterChildren",
duration: 2
}
})
};
export const Content = styled(motion.div)`
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
background-color: burlywood;
padding: 30px;
height: 500px;
`;
export const ToggleBtn = styled.button`
padding: 5px 10px;
cursor: pointer;
display: flex;
width: auto;
align-self: flex-end;
`;
export const ImgVariants = {
expanded: {
width: "100px",
scale: 1,
transition: {
duration: 2
}
},
collapsed: {
scale: 0.5,
transition: {
duration: 2
}
}
};
const Img = styled(motion.img)``;
class App extends Component {
state = {
collapsed: false
};
toggle = () => {
this.setState({ collapsed: !this.state.collapsed });
};
render() {
const { collapsed } = this.state;
return (
<div>
<Content
initial={collapsed ? "collapsed" : "expanded"}
animate={collapsed ? "collapsed" : "expanded"}
variants={ContentVariants}
>
<Img
src="https://picsum.photos/200/200"
initial={collapsed ? "collapsed" : "expanded"}
animate={collapsed ? "collapsed" : "expanded"}
variants={ImgVariants}
/>
<ToggleBtn onClick={this.toggle}>toggle</ToggleBtn>
</Content>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
如果我在 ContentVariants
中将 when: "afterChildren"
更改为 when: "beforeChildren"
,它没有任何区别。即使我删除 when
属性,动画也会同时 运行。
文档传播部分 (https://www.framer.com/api/motion/animation/#propagation) 说
If a motion component has children, changes in variant will flow down through the component hierarchy. These changes in variant will flow down until a child component defines its own animate property.
您必须从 Img
个元素中删除 animate
属性。
如果您在 child 上设置动画,您的 parent 将不会将其动画逻辑传递给它。因此,您必须从 <Img>
组件中删除 initial
和 animate
属性:
<Img
src="https://picsum.photos/200/200"
variants={ImgVariants}
/>
您可以从官方文档中查看此示例以供参考:https://www.framer.com/api/motion/types/#orchestration.when