javascript - 对象在 React 的 IE11 中不支持属性或方法 'scrollBy'

标签 javascript reactjs internet-explorer-11 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 });
  }

除 IE 11 外,这在任何地方都有效。在 IE 11 中,我收到以下错误:

TypeError: Object doesn't support property or method 'scrollBy'

当我控制台记录元素时,document.getElementById 确保我选择了元素。

我正在使用 babel-polyfil

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

最佳答案

Babel polyfill “将模拟完整的 ES2015+ 环境”。然而,scrollBy不是 ECMAScript 规范的一部分,因此不会被 Babel 填充。 你需要自己添加合适的polyfill,比如smooth scroll behaviour polyfill其中还包括 scrollBy

关于javascript - 对象在 React 的 IE11 中不支持属性或方法 'scrollBy',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52793371/

相关文章:

jquery - 使用 jQuery.css 在 Internet Explorer 中获取值

javascript - IE11 和 JavaScript array.map()

javascript - 单击 map 自定义控件时获取特定子项

javascript - parse.com 具有高级定位功能的推送通知

javascript - 如何设置本地存储的长度限制?

javascript - 如果状态是 Set 类型,如何保持状态不可变?如何复制 javascript Set 类型?

javascript - 如何动态加载位于外部服务器中的 JavaScript 文件?

javascript - React JS 中通过 id 链接

javascript - Overlay 比 React Native 中的父样式组件小

Javascript Prompt() 文本在 Internet Explorer 11 中部分隐藏