javascript - 动画在 react-spring 中过渡时的组件捕捉效果?

标签 javascript css reactjs react-spring

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

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

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

最佳答案

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

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

希望对你有帮助

关于javascript - 动画在 react-spring 中过渡时的组件捕捉效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55045792/

相关文章:

javascript - 与全局 'let' 不同,javascript 'window' 全局变量不是 'var' 的属性

javascript - jquery 选取框不能在 chrome 中工作

javascript - 粘性页脚内的粘性页脚

javascript - Popover 组件 - onExited 回调不起作用, Material ui

reactjs - 添加自定义 Service Worker 到 create-react-app

javascript - 当调用其他 redux 操作创建者时,Redux 表单不会显示在 React Bootstrap Modal 上

javascript - YUI 有官方的 irc channel 吗?

javascript - 我可以使用哪些方法在 Internet Explorer 中禁用 Alt + *X*?

javascript - Angular.js 在 ng-repeat 中获取二维数组中最后一个元素的索引

javascript - 在选择某些东西之前隐藏 d3.chart 的详细信息