【问题标题】:react-spring showing jerky movements in a transitionreact-spring 在过渡中显示生涩的动作
【发布时间】:2020-03-04 17:31:20
【问题描述】:

我正在使用react-spring 创建一个简单的画廊动画。我希望图像从右侧出现并以平滑的过渡向左侧离开。但是,我似乎无法摆脱 here 所示的生涩动作。

代码如下:

import React, { useState, useEffect } from 'react'
import { useTransition, animated } from 'react-spring'

import '../style/Gallery.css'

const pages = [
    ({ style }) => <animated.div style={{ ...style, background: 'lightpink' }}>A</animated.div>,
    ({ style }) => <animated.div style={{ ...style, background: 'lightblue' }}>B</animated.div>,
    ({ style }) => <animated.div style={{ ...style, background: 'lightgreen' }}>C</animated.div>,
]

const Gallery = () => {
    const [index, set] = useState(0)

    const transitions = useTransition(index, p => p, {
        from: { opacity: 0, transform: 'translate3d(100%,0,0)' },
        enter: { opacity: 1, transform: 'translate3d(0%,0,0)' },
        leave: { opacity: 0, transform: 'translate3d(-50%,0,0)' },
    })

    useEffect(() => void setInterval(() => set(state => (state + 1) % 3), 2000), [])

    return (
        <div className="gallery">
            {transitions.map(({ item, props, key }) => {
                const Page = pages[item]
                return <Page key={key} style={props} />
            })}
        </div>
    )
}

export default Gallery

【问题讨论】:

    标签: reactjs react-native animation react-animated react-spring


    【解决方案1】:

    当离开的元素退出时,它的空白空间会导致抽搐。尝试绝对定位。

      from: { position:'absolute', opacity: 0, transform: 'translate3d(100%,0,0)' },
    

    【讨论】:

      猜你喜欢
      • 2014-12-04
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多