代码之家  ›  专栏  ›  技术社区  ›  Finglish

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

  •  1
  • Finglish  · 技术社区  · 6 年前

    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'
    

    1 回复  |  直到 6 年前
        1
  •  5
  •   str    6 年前

    Babel polyfill “将模拟完整的ES2015+环境”。然而, scrollBy 例如,您需要自己添加一个合适的polyfill smooth scroll behaviour polyfill 其中还包括 .

        2
  •  1
  •   Aaron Plocharczyk    4 年前

    只有 填写 scrollBy

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

    …并将其添加到我的页面的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);
      };
    }