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

后端 未结 7 1423
心在旅途
心在旅途 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:03

    This is just a random one, but possible doing something like this:

    
    

    The thinking being that if it is some focus stealing script on a remote page that you can't control, the browser won't focus a hidden element. And there's a good likelihood that your onload will fire after their focus changing script.

    Or, one other option that might be a bit more reliable:

    
    

    Here we're basically saying hiding the frame and moving it to a negative offset on the page vertically. When it does try to focus the element inside of the frame, it should scroll the page upward, then once loaded place the iframe back in it's intended position.

    Of course, without knowing more, it's hard to say for sure which tradeoffs are okay, and both of these options have conditions that are a tad racy, so YMMV.

    I hope that helps :)

提交回复
热议问题