我試圖在發生點擊事件時發生背景顏色轉換,並且沒有進一步的交互,在1或2秒後淡出背景顏色。使用onClick淡入淡出轉換
基本上我想要做什麼active
屬性在CSS中,但對於點擊事件。
我目前的方法需要點擊事件第二次淡出背景顏色。我怎樣才能在點擊
我的做法
handleClick(id) {
this.setState({
active: !this.state.active
})
}
<div className={this.state.active ? "txt_vote_bar_div txt_vote_bar_div_active" : "txt_vote_bar_div txt_vote_bar_div_notactive"}
onClick={this.handleClick()}>
</div>
我的CSS
.txt_vote_bar_div {
width: 100%;
height: 50px;
min-height: 50px;
position: relative;
border: 1px solid #C6C6C6;
border-radius: 5px;
margin-bottom: 10px;
cursor: pointer;
}
.txt_vote_bar_div_active {
background-color: #001f3f;
transition: 1s ease-in-out;
}
.txt_vote_bar_div_notactive {
background-color: #FFFFFF;
transition: 1s ease-in-out;
}
您正在調用處理函數而不是傳遞一個ref。這個'onClick = {this.handleClick()}'應該改爲這個'onClick = {this.handleClick}' –
@CraZyDroiD請找到我的答案,並讓我知道你是否有任何問題。 –