最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Object doesn't support property or method 'scrollBy' in IE11 in React - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Oct 13, 2018 at 13:22 FinglishFinglish 9,99615 gold badges77 silver badges118 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

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.

I'm over a year late to this question, but if anyone else is facing this issue and wants a solution that only fills in scrollBy, I'm using jQuery:

<script src="https://ajax.googleapis./ajax/libs/jquery/3.4.1/jquery.min.js"></script>

...and adding this to my page's JavaScript:

if(!window.scrollBy){
  window.scrollBy = function(x, y){
    if(x) $('html, body').scrollLeft($(window).scrollLeft() + x);
    if(y) $('html, body').scrollTop($(window).scrollTop() + y);
  };
}

if(!Element.prototype.scrollBy){
  Element.prototype.scrollBy = function(x, y){
    if(x) $(this).scrollLeft($(this).scrollLeft() + x);
    if(y) $(this).scrollTop($(this).scrollTop() + y);
  };
}

It's tested and working in IE 11.

发布评论

评论列表(0)

  1. 暂无评论