0

每次更改图片组件时,我都会尝试制作动画。页面加载时的第一个动画很好。当我单击“+”按钮时,动画会突然突然回到其最终位置并且不流畅。我不明白为什么会发生这种快照效果。我该如何解决 ?

const PictureArt = (props) => {
    const imageStyle = {
        width: '100%',
        height: 'auto',
        borderRadius: '5px',
        boxShadow: '5px 5px 20px',
    }


    const [items, setItems] = useState([{
    url: 'http://4.bp.blogspot.com/-mZwB8lpO3-0/UN1r7ZrbjnI/AAAAAAAADBU/rqa5a2DD_KY/s1600/White_Flower_Closeup.jpg',
    caption: 'flower',
    description: "When the flower looked beautiful ... "
}, {
    url: 'http://4.bp.blogspot.com/-mZwB8lpO3-0/UN1r7ZrbjnI/AAAAAAAADBU/rqa5a2DD_KY/s1600/White_Flower_Closeup.jpg',
    caption: 'flower',
    description: "When the flower looked beautiful ... "
}]);

    const [currentIndex, setCurrentIndex] = useState(0);

    const totalItems = items.length ; 

    const picTransition = useTransition(currentIndex , null, {
        from: { opacity: '0', transform: `translate3d(100px,0px,0)` },
        enter: { opacity: '1', transform: 'translate3d(0,0,0)' },
        leave: { opacity: '0', transform: `translate3d(-100px , 0px , 0)` },
    })
    const currentItem = items[currentIndex] ; 

    return (
        <>
            <button onClick={e=>setCurrentIndex((currentIndex+1)%totalItems)}>"+"</button>
            <div>
                <div className="container">
                    {
                        picTransition.map(({ item, props, key }) => {
                            return (
                                <animated.div style={props} key={key}>
                                    <div style={{ width: '500px' }}>
                                        <img src={currentItem.url} style={imageStyle}  ></img>
                                    </div>
                                </animated.div>)
                        })
                    }

                </div>
            </div>
        </>
    );
}

function App() {
  return (
    <div className="App">
      <h1>Click below + button</h1>
      <PictureArt/>
    </div>
  );
}

这是一个代码框片段: https ://codesandbox.io/s/wn6jp23vxl

4

1 回答 1

2

我遇到了同样的问题,我position: absolute通过divimg. 在分析了 react-spring 的创建者的沙箱之后,我这样做了:

<div style={{ width: "500px", position: "absolute" }}>
  <img src={currentItem.url} style={imageStyle} />
</div>

希望能帮助到你

于 2019-04-18T15:02:39.513 回答