我正在使用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发布于 2019-11-08 14:24:20
当离开元素展开时,它的空格会导致抖动。尝试绝对定位。
from: { position:'absolute', opacity: 0, transform: 'translate3d(100%,0,0)' },https://stackoverflow.com/questions/58760524
复制相似问题