我每次更改图片组件时都会尝试使用动画。页面加载时的第一个动画很好。当我单击" "按钮时,动画会捕捉到混蛋中的最终位置,但并不光滑。我不明白为什么会发生这种快照效果。我该如何解决 ?
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
添加到div
包装img
来修复您的沙箱。在分析了来自React-Spring的创建者的沙箱之后,我做到了:
<div style={{ width: "500px", position: "absolute" }}>
<img src={currentItem.url} style={imageStyle} />
</div>
希望它有帮助