gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:34 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com