[英]Hold button to scroll continuously ReactJS
我有一個div左右兩個按鈕,這個div有一個滾動條,我想要做的就是,當按住一個按鈕1000ms時......滾動條不斷移動(就像我們瀏覽器中的默認實現一樣)我知道如何使用setTimeout! 但問題出在功能內部
在課堂上
holdRightButton = () => {
// i want to use this element :
// const slider : HTMLElement = this.state.scrollRef.current;
this.setState({
timer : setTimeout(() => {
}, 1000)
})
}
releaseButton = () => {
clearTimeout(this.state.timer);
}
里面的渲染():
<Button onMouseDown={this.holdRightButton}
onMouseUp={this.releaseButton}>
<span className="fas fa-chevron-right" />
</Button>
我試圖做例如Element.ScrollBy(1,0),但是你知道..它只會執行一次我也嘗試了一個布爾值,當發送事件onMouseUp但應用程序崩潰任何解決方案時轉向False? 即使它完全不同
setTimeout只執行一次。
請使用setInterval和clearInterval
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.