首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-spring在过渡中显示抖动的运动

react-spring在过渡中显示抖动的运动
EN

Stack Overflow用户
提问于 2019-11-08 12:47:46
回答 1查看 871关注 0票数 1

我正在使用react-spring创建一个简单的图库动画。我希望图像从右侧出现,并在平滑过渡中向左侧离开。然而,我似乎无法摆脱here所显示的抖动。

代码如下:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-08 14:24:20

当离开元素展开时,它的空格会导致抖动。尝试绝对定位。

代码语言:javascript
复制
  from: { position:'absolute', opacity: 0, transform: 'translate3d(100%,0,0)' },
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58760524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档