简体   繁体   English

如何为成帧器运动添加条件 animation

[英]How to add condition to framer motion animation

I'm new to react and i made three cards and i used framer motion for the animation.我是新手,我制作了三张卡片,并为 animation 使用了成帧器动作。 i want when i click the one cards the animation start but the animation start on all the three cards how can i add a condition to framer motion animation?我想当我单击一张卡时 animation 启动但 animation 在所有三张卡上启动我如何为成帧器运动添加条件 animation?

sandbox link: https://codesandbox.io/s/angry-microservice-tllp80?file=/src/App.js沙盒链接: https://codesandbox.io/s/angry-microservice-tllp80?file=/src/App.js

and here is my code这是我的代码

import "./styles.css";
import { useState,useEffect } from "react";
import { motion, useAnimationControls } from "framer-motion"


export default function App() {
  const [elements,setElements]=useState(['one','two','three'])
  const controls = useAnimationControls()


  return (
    <section className="App">
      {
        elements.map((Element,index)=>(
          <motion.div key={index}  animate={controls} onClick={()=>controls.start({ scale: 2 })}>{Element}</motion.div>
        ))
      }   
    </section>
  );
} 

What you need to do is create a component that handles the animation control itself and use it on elements map您需要做的是创建一个组件来处理 animation 控件本身并将其用于元素 map

Here is an example:这是一个例子:

import "./styles.css";
import { useState,useEffect } from "react";
import { motion, useAnimationControls } from "framer-motion"


const Card = ({index, Element}) => {

  const controls = useAnimationControls()

  return <motion.div key={index}  animate={controls} onClick={()=>controls.start({ scale: 2 })}>{Element}</motion.div>

}


export default function App() {
  const [elements,setElements]=useState(['one','two','three'])
 

  return (
    <section className="App">
      {
        elements.map((Element,index)=><Card index={index} Element={Element}/>)
      }   
    </section>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM