对象在 React 的 IE11 中不支持属性或方法“scrollBy”

Fin*_*ish 3 javascript internet-explorer-11 reactjs babel-polyfill

我正在使用以下代码在 mouseDown 上滚动滚动元素,并在 mouseUp/mouseOut 上停止滚动。

scrubby(xDir) {
    let dub = setInterval(() => {
      document.getElementById("chart").scrollBy(8 * xDir, 0);
    }, 10);

    this.setState({ dub: dub });
  }

  scrubbyStop() {
    const { dub } = this.state;
    if (dub !== null) {
      clearInterval(dub);
    }
    this.setState({ dub: null });
  }
Run Code Online (Sandbox Code Playgroud)

这适用于除 IE 11 以外的所有地方。在 IE 11 中,我收到以下错误:

TypeError: Object doesn't support property or method 'scrollBy'
Run Code Online (Sandbox Code Playgroud)

当我 console.log 元素时,document.getElementById 以确保我选择了元素。

我正在使用 babel-polyfil

我看到很多与 scrollTo 相关的问题,但不是 scrollBy。有谁知道可能适用于 IE 的任何变通方法、polyfill 或替代方案。

str*_*str 5

Babel polyfill“将模拟完整的 ES2015+ 环境”。但是,scrollBy它不是 ECMAScript 规范的一部分,因此不会被 Babel 填充。您需要自己添加适当的 polyfill,例如平滑滚动行为 polyfill,其中还包括scrollBy.