每次图像组件更改时,我都试图制作动画。页面加载时的第一个动画很好。当我单击“ +”按钮时,动画会突然跳回到其最终位置,并且不流畅。我不明白为什么会发生这种突然的影响。我该如何解决 ?
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>
);
}
这是一个codeandbox片段:https://codesandbox.io/s/wn6jp23vxl
答案 0 :(得分:1)
我也遇到了同样的问题,因此我在包裹position: absolute
的{{1}}上添加了div
来修复了您的沙箱。在分析了react-spring的创建者的sandbox之后,我做到了:
img
希望有帮助