Kevvv
Kevvv

Reputation: 4023

react-spring showing jerky movements in a transition

I'm using react-spring to create a simple gallery animation. I want the images to appear from the right and leave towards the left in a smooth transition. However, I can't seem to get rid of the jerky motion as shown here.

The code is as follows:

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

Upvotes: 1

Views: 1894

Answers (1)

Peter Ambruzs
Peter Ambruzs

Reputation: 8213

When the leaving element unmouts, its empty space causes the jerking. Try absolute positioning.

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

Upvotes: 4

Related Questions