你试过useSpring
吗?
const Component = () => {
const scale = useSpring(1);
return (
<motion.div
style={{ scale, background: "tomato", width: "100px", height: "100px" }}
onMouseEnter={() => scale.set(1.35)}
onMouseLeave={() => scale.set(1)}
/>
);
};
文档:https ://www.framer.com/api/motion/motionvalue/#usespring
或者您也可以使用useAnimation
创建自定义控件和过渡:
const Component2 = () => {
const controls = useAnimation();
return (
<motion.div
style={{ background: "blue", width: "100px", height: "100px" }}
animate={controls}
onMouseEnter={() => controls.start({ scale: 1.35 })}
onMouseLeave={() => controls.start({ scale: 1 })}
/>
);
};
文档:https ://www.framer.com/api/motion/animation/#animation-controls
带有两个示例的 Codesandbox:https ://codesandbox.io/s/httpsstackoverflowcomquestions64077992-j63qv?file=/src/App.js