首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >framer.motion动画是即时的,而不是动画

framer.motion动画是即时的,而不是动画
EN

Stack Overflow用户
提问于 2021-02-18 22:47:23
回答 2查看 558关注 0票数 0

我有几个盒子,我想通过它们来做动画,

下面是一个简单的应用程序示例(也是一个codesandbox here)

每个“长方体”应该淡入和淡出,然而,在这个例子中,动画是瞬间发生的。

代码语言:javascript
复制
const Box = styled.div`
  width: 100px;
  height: 100px;
  background: green;
`;

const Test = ({ isActive }) => {
  return (
    <motion.div
      animate={isActive ? { opacity: 1 } : { opacity: 0 }}
      transition={{ duration: 3 }}
    >
      <Box>hello world</Box>
    </motion.div>
  );
};

export default function App() {
  const [currentIndex, setCurrentIndex] = useState(0);

  const boxes = [
    {
      component: ({ isActive }) => <Test isActive={isActive} />
    },
    {
      component: ({ isActive }) => <Test isActive={isActive} />
    }
  ];

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <div onClick={() => setCurrentIndex(currentIndex === 0 ? 1 : 0)}>
        {boxes.map((box, index) => {
          const isActive = index === currentIndex;
          return <box.component isActive={isActive} />;
        })}
      </div>
    </div>
  );
EN

回答 2

Stack Overflow用户

发布于 2021-02-18 23:25:04

我以前从未使用过framer.motion,但查看他们的文档后,我认为您可以使用变体来实现您所需的功能。https://www.framer.com/api/motion/examples/

我稍微重构了你的代码,让它正常工作:

代码语言:javascript
复制
import "./styles.css";
import { motion } from "framer-motion";
import styled from "styled-components";
import { useEffect, useState } from "react";

const Box = styled.div`
  width: 100px;
  height: 100px;
  background: green;
`;

const variants = {
  open: { opacity: 1 },
  closed: { opacity: 0 }
};

const Test = ({ index, currentIndex }) => {
  return (
    <motion.div
      animate={index === currentIndex ? "open" : "closed"}
      variants={variants}
      transition={{ duration: 3 }}
    >
      <Box>hello world</Box>
    </motion.div>
  );
};

export default function App() {
  const [currentIndex, setCurrentIndex] = useState(0);
  const boxes = ["a", "b"];

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>{currentIndex}</h2>
      <div onClick={() => setCurrentIndex(currentIndex === 0 ? 1 : 0)}>
        {boxes.map((box, i) => {
          return <Test index={i} currentIndex={currentIndex} />;
        })}
      </div>
    </div>
  );
}

currentIndex作为道具传递给子测试组件,它们检查自己的索引是否与currentIndex匹配,并相应地更新它们的动画。

编辑的代码和框在这里:https://codesandbox.io/s/suspicious-austin-tymvx

票数 0
EN

Stack Overflow用户

发布于 2021-04-15 17:45:24

在帧运动中,您具有useCycle属性。这是an example

示例中的代码:

代码语言:javascript
复制
import * as React from "react";
import { render } from "react-dom";
import { Frame, useCycle } from "framer";
import "./styles.css";

export function MyComponent() {
  const [animate, cycle] = useCycle(
    { scale: 1.5, rotate: 0, opacity: 1 },
    { scale: 1.0, rotate: 90, opacity: 0 }
  );
  return (
    <Frame
      animate={animate}
      onTap={() => cycle()}
      size={150}
      radius={30}
      background={"#fff"}
    />
  );
}

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

和一些简单的css:

代码语言:javascript
复制
body {
  margin: 0;
  padding: 0;
}

#root {
  font-family: sans-serif;
  text-align: center;
  width: 100vw;
  height: 100vh;
  display: flex;
  place-content: center;
  place-items: center;
  background: rgba(0, 85, 255, 1);
  margin: 0;
  padding: 0;
  perspective: 1000px;
}

我不建议你使用这种类型的结构:animate={index === currentIndex ? "open" : "closed"},因为你可能会有一些滞后/中断的动画。请始终尝试搜索MotionAPI库的示例/元素。您将拥有更少的代码行,并且大部分代码是“干净”的,没有无用的变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66262295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档