【问题标题】:How to animate number with Framer Motion?如何使用 Framer Motion 为数字设置动画?
【发布时间】:2020-11-25 22:45:46
【问题描述】:

我正在将应用程序从 react-spring 迁移到 framer-motion。使用 Spring,我可以使用以下方法为数字设置动画:

export default function MyNumber({ number }) {
  const spring: any = useSpring({ from: { number: 0 }, to: { number } });
  return (
    <animated.div>
      {spring.number.interpolate((c) => c.toFixed(1))}
    </animated.div>
  );
}

我不知道如何使用 Framer-Motion。

【问题讨论】:

    标签: reactjs framer-motion


    【解决方案1】:

    所以我找到了答案:

    import { animate } from "framer-motion";
    import React, { useEffect, useRef } from "react";
    
    function Counter({ from, to }) {
      const ref = useRef();
    
      useEffect(() => {
        const controls = animate(from, to, {
          duration: 1,
          onUpdate(value) {
            ref.current.textContent = value.toFixed(1);
          }
        });
        return () => controls.stop();
      }, [from, to]);
    
      return <p ref={ref} />;
    }
    
    export default function App() {
      return <Counter from={0} to={65.4} />;
    }

    【讨论】:

    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 2019-11-29
    • 2021-07-06
    • 1970-01-01
    • 2021-04-04
    • 2022-01-15
    • 2023-02-03
    • 2023-02-10
    相关资源
    最近更新 更多