iframe on the page bottom: avoid automatic scroll of the page

后端 未结 7 1425
心在旅途
心在旅途 2020-12-25 13:46

I have an iframe from the middle to bottom on a page. When I load the page it scrolls to the bottom. I tried to body onload window.scroll(0,0) but

相关标签:
7条回答
  • 2020-12-25 14:06

    I came up with a "hack" that works well. Use this if you don't want your webpage to be scrolled to anywhere except the top:

    // prevent scrollTo() from jumping to iframes
    var originalScrollTo = window.scrollTo;
    
    window.scrollTo = function scrollTo (x, y) {
      if (y === 0) {
        originalScrollTo.call(this, x, y);
      }
    }
    

    If you want to disable autoscrolling completely, just redefine the function to a no-op:

    window.scrollTo = function () {};
    
    0 讨论(0)
提交回复
热议问题