Search code examples
javascriptreactjsinternet-explorer-11babel-polyfill

Object doesn't support property or method 'scrollBy' in IE11 in React


I am using the following code to scroll scroll an element on mouseDown, and stop scrolling on 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 });
  }

This works everywhere except IE 11. In IE 11 I get the following error:

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

When I console.log the element, the document.getElementById to ensure I am selecting the element.

I am using babel-polyfil

I see a lot of questions related to scrollTo, but not scrollBy. Does anyone know any workarounds, polyfill or alternatives that might work for IE.


Solution

  • Babel polyfill "will emulate a full ES2015+ environment". However, scrollBy is not part of the ECMAScript specification and thus will not be polyfilled by Babel. You need to add a proper polyfill by yourself, for example smooth scroll behaviour polyfill which also includes scrollBy.