我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