【问题标题】:component snapping effect when animation transitions out in react-spring?react-spring中动画过渡时的组件捕捉效果?
【发布时间】:2019-07-29 10:59:07
【问题描述】:

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

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>
  );
}

这是一个代码框 sn-p:https://codesandbox.io/s/wn6jp23vxl

【问题讨论】:

    标签: javascript css reactjs react-spring


    【解决方案1】:

    我遇到了同样的问题,我通过将 position: absolute 添加到 div 包装 img 来修复您的沙盒。在分析了 react-spring 的创建者 sandbox 之后,我这样做了:

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-30
      • 2020-02-20
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      相关资源
      最近更新 更多