【发布时间】:2019-07-19 07:33:24
【问题描述】:
我正在尝试更改每 1 秒显示的图像,第一个图像出现然后切换到 alt 显示并且不继续切换图片
export default class Slideshow extends Component {
constructor(props) {
super(props);
this.getImageId = this.getImageId.bind(this);
this.switchImage = this.switchImage.bind(this);
this.init = this.init.bind(this);
this.state = {
currentImage: 0,
image: 0
};
}
getImageId() {
if(this.currentImage < 3) {
this.setState({
currentImage: this.state.currentImage +1
})
} else {
this.setState({
currentImage: 0
})
}
return this.currentImage;
}
switchImage() {
this.setState({
image: this.getImageId()
});
}
init() {
setInterval(this.switchImage, 1000)
}
render() {
const imagePath = [guy, girl, wash, swifer];
this.init();
return (
<div className="slideshow-container">
<img src={imagePath[this.state.image]} alt="cleaning images"/>
</div>
);
}
}
图片每1秒切换到数组中的下一张图片,遍历整个数组后返回原始图片
【问题讨论】:
-
嗨,贾斯汀,我刚刚在下面为您提供了一个解决方案,它应该让您对如何集成此功能有足够的了解。如果您有任何问题,请告诉我,请考虑投票并将其标记为答案:)
标签: javascript html arrays reactjs setinterval