简体   繁体   English

将 div 样式作为函数参数传递 Javascript/React

[英]Passing div style as a function argument Javascript/ React

I'm building an application using React, framer-motion and react-intersection-observer.我正在使用 React、framer-motion 和 react-intersection-observer 构建一个应用程序。 Inside animation.js I have a function which is imported inside App.js and used as a component in App.js.在animation.js 中,我有一个函数,它被导入到App.js 中并用作App.js 中的组件。

I want to apply an aspect ratio to some divs using style as a parameter, but it doesn't work.我想使用样式作为参数对某些 div 应用纵横比,但它不起作用。

<FadeAnimation name={'project-image--image'} style={'--ratio':16/9} />



Failed to set an indexed property on 'CSSStyleDeclaration': Indexed property setter is not supported.

I have other divs with this property and they are displayed correctly我有其他具有此属性的 div 并且它们显示正确

<div className='project-image--image' style={{'--ratio':1/2}}/>

Animation.js动画.js

export const container = {
  hidden: { opacity: 0, y: 5 },
  visible: { opacity: 1, y: 0 }
}

function FadeAnimation({ children, name, delay, duration, style }) {
  const controls = useAnimation();
  const [ref, inView] = useInView();

  useEffect(() => {
    if (inView) {
      controls.start("visible");
    }
  }, [controls, inView]);

  return (
    <motion.div className={`${name}`} style={{`${style}`}}
      ref={ref}
      animate={controls}
      initial="hidden"
      transition={{ duration: duration, delay: delay }}
      variants={{
        visible: { opacity: 1, y: 0 },
        hidden: { opacity: 0, y: 5 }
      }}
    >
      {children}
    </motion.div>
  );
}

Have you tried:你有没有尝试过:

<FadeAnimation name={'project-image--image'} style={{'--ratio':16/9}} />

(Adding another curly brace) (添加另一个花括号)

And then, in the FadeAnimationComponent using it as然后,在FadeAnimationComponent中使用它作为

<motion.div className={`${name}`} style={style} {/*...*/}/>

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

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