【问题标题】:Hold button to scroll continuously ReactJS按住按钮以连续滚动 ReactJS
【发布时间】:2019-10-28 14:07:49
【问题描述】:

我有两个按钮左右到一个 div,这个 div 有一个滚动条 我想要做的是,当按住一个按钮 1000 毫秒时 .. 滚动条不断移动(就像我们浏览器中的默认实现一样) 我知道如何使用 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) 但你知道 .. 它只会执行 1 次 我还尝试了一个布尔值,当 onMouseUp 事件被发送但应用程序崩溃时变为 False 有什么解决办法吗?即使完全不同

【问题讨论】:

    标签: javascript html css reactjs typescript


    【解决方案1】:

    setTimeout 只做一次。

    请使用 setInterval 和 clearInterval

    https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      相关资源
      最近更新 更多