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