1

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

代码如下:

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
4

1 回答 1

2

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

  from: { position:'absolute', opacity: 0, transform: 'translate3d(100%,0,0)' },
于 2019-11-08T06:24:20.480 回答