简体   繁体   English

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

[英]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.我正在使用以下代码在 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 });
  }

This works everywhere except IE 11. In IE 11 I get the following error:这适用于除 IE 11 以外的所有地方。在 IE 11 中,我收到以下错误:

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.当我 console.log 元素时,document.getElementById 以确保我选择了元素。

I am using babel-polyfil我正在使用 babel-polyfil

I see a lot of questions related to scrollTo, but not scrollBy.我看到很多与 scrollTo 相关的问题,但不是 scrollBy。 Does anyone know any workarounds, polyfill or alternatives that might work for IE.有谁知道可能适用于 IE 的任何变通方法、polyfill 或替代方案。

Babel polyfill "will emulate a full ES2015+ environment". Babel polyfill“将模拟完整的 ES2015+ 环境”。 However,scrollBy is not part of the ECMAScript specification and thus will not be polyfilled by Babel.然而,scrollBy不是 ECMAScript 规范的一部分,因此不会被 BabelscrollBy You need to add a proper polyfill by yourself, for example smooth scroll behaviour polyfill which also includes scrollBy .您需要自己添加适当的 polyfill,例如平滑滚动行为scrollBy ,其中还包括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:我对这个问题晚了一年多,但是如果其他人面临这个问题并且想要一个填写scrollBy的解决方案,我正在使用 jQuery:

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

...and adding this to my page's JavaScript: ...并将其添加到我页面的 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.它已在 IE 11 中测试和运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IE11:对象不支持属性或方法“ replaceWith” - IE11: Object doesn't support property or method 'replaceWith' 对象不支持属性或方法“ readAsBinaryString”-IE11 - Object doesn’t support property or method ‘readAsBinaryString’ - IE11 对象不支持IE11中的属性或方法&#39;getElementsById&#39; - Object doesn't support property or method 'getElementsById' in IE11 IE11 对象不支持属性或方法“规范化” - IE11 Object doesn't support property or method 'normalize' IE11 - 对象不支持属性或方法“包含” - IE11 - Object doesn't support property or method 'contains' Object 不支持 IE11 中的属性或方法“forEach” - Object doesn't support property or method 'forEach' in IE11 对象不支持属性或方法IE11(兼容模式)-Knockout.js - Object doesn't support property or method IE11 (Compatibility Mode) - knockoutjs TypeError:对象不支持属性或方法“条目”(IE11) - TypeError: Object doesn't support property or method 'entries' (IE11) 对象不支持属性或方法“删除” IE11(react.js) - Object doesn't support property or method 'delete' ie11 (reactjs) IE11/Windows 10 =&gt; 对象不支持属性或方法“包含” - IE11/Windows 10 => Object doesn't support property or method 'includes'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM