你好我試圖做一個圖像轉換器。在第一次加載時,我應該顯示狀態中的第一個圖像。如果點擊按鈕,圖像應該更改爲第二個,依此類推。現在的問題是獲得與按鈕ReactJS加載點擊圖片
顯示兩個圖像,這裏是我斌:enter link description here
你好我試圖做一個圖像轉換器。在第一次加載時,我應該顯示狀態中的第一個圖像。如果點擊按鈕,圖像應該更改爲第二個,依此類推。現在的問題是獲得與按鈕ReactJS加載點擊圖片
顯示兩個圖像,這裏是我斌:enter link description here
因爲你顯示您的渲染圖像2。
繼承人一個想法如何做到這一點(沒有動畫,我不知道CSSTransitionGroup組件)。只需更換您的main.js代碼的任意不等階以下
import React from 'react'
import {render} from 'react-dom'
import { CSSTransitionGroup } from 'react-transition-group'
class FadeImage extends React.Component {
constructor(props) {
super(props);
this.state = {
images: [
'http://via.placeholder.com/350x150',
'http://via.placeholder.com/350x151'
],
currentImage: 0
};
}
fadeImage(e) {
e.preventDefault();
this.setState({currentImage: (this.state.currentImage + 1) % this.state.images.length})
}
render() {
return (
<div className="image">
<CSSTransitionGroup
transitionName="example"
transitionEnterTimeout={300}
transitionLeaveTimeout={300}
>
<section>
<button className="button" onClick={this.fadeImage.bind(this)}>Click!</button>
<img src={this.state.images[this.state.currentImage]}/></section>
</CSSTransitionGroup>
</div>
);
}
}
render(<FadeImage />, document.querySelector('#app'))
http://bl.ocks.org/piotrf/1b7b14b0c2bfbfe1f1ef這幾乎正是你想要的。你可以擺脫你不需要的部分,並將按鈕更改爲你想要的圖像
這正是我想要的謝謝你:) – Alex