简体   繁体   English

React Framer motion animation 在开发中有效,但在生产构建中无效

[英]React Framer motion animation works in development but doesn't work in production build

Here, in the production build the opacity and translateY values are stuck to their default values and wont change with scrolling, whereas they do change with scrolling in the development build.在这里,在生产版本中,opacity 和 translateY 值固定为默认值,不会随着滚动而改变,而在开发版本中它们会随着滚动而改变。

const Subcard = ({title, text, index, scrollDiv})=>{
  const myRef = useRef(null)
  const { scrollYProgress } = useScroll({
    container: scrollDiv,
    target: ref,
    offset: ["start end", "end 0.75"]
  })

  const opacity = useTransform(scrollYProgress, [0,1], [0,1])
  const translateY = useTransform(scrollYProgress, [0, 1], [600, 0])

  return(
    <motion.div
      ref = {myRef}
      style={{
        padding:'2rem', 
        borderRadius:'0.25rem',
        paddingBottom:'2rem',
        backgroundColor:'rgb(235,235,235,0.1)',
        marginRight:'1.5rem',
        marginLeft:'1.5rem',
        marginTop:`${index*5}rem`,
        lineHeight:'1.75rem',
        letterSpacing:'0.075rem',
        translateY,
        opacity
      }}
    >
      <div style={{fontSize:'1.5rem', marginBottom:'1rem'}}>{title}</div>
      <div>{text}</div>
    </motion.div>
  )
}

After some trial and error I could replicate this in the development server if I remove the line: container: scrollDiv经过反复试验,如果删除以下行,我可以在开发服务器中复制它: container: scrollDiv

But I couldn't figure out how to resolve this issue但我不知道如何解决这个问题

Hey I was going nuts because of the same problem.嘿,我因为同样的问题而发疯了。 So you're doing exactly what I did, passing the container ref in the prop .所以你正在做我所做的,在prop中传递容器ref In order for this combination to work, add layoutEffect: false, in useScroll 's first parameter ( UseScrollOptions ).为了使此组合起作用,请在useScroll的第一个参数 ( UseScrollOptions ) 中添加layoutEffect: false,

const { scrollYProgress } = useScroll({
    layoutEffect: false,
    container: scrollDiv,
    target: ref,
    offset: ["start end", "end 0.75"]
  })

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

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