页面底部的iframe:避免自动滚动页面 [英] iframe on the page bottom: avoid automatic scroll of the page

查看:93
本文介绍了页面底部的iframe:避免自动滚动页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面的中间到底部有 iframe 。当我加载页面时,它会滚动到底部。我尝试 body onload window.scroll(0,0)但是它会产生一种难看的效果,因为它先下降然后立即向上滚动。

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 it does an ugly effect because it first goes down and then immediately scrolls up.

页面上 iframe 自动滚动到底部的原因是什么?

What's the cause of this automatic scroll to bottom with iframe on the page?

推荐答案

这只是一个随机的,但可能做这样的事情:

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

<iframe style="display: none;" onload="this.style.display='block';" href="..."></iframe>

这个想法是,如果它是一个焦点窃取远程页面上无法控制的脚本,浏览器不会聚焦隐藏元素。并且很有可能在你的焦点改变脚本之后你的onload会被激活。

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:

<iframe style="position: absolute; top: -9999em; visibility: hidden;" onload="this.style.position='static'; this.style.visibility='visible';" href="..."></iframe>

这里我们基本上是说隐藏框架并将其垂直移动到页面上的负偏移。当它确实尝试将元素聚焦在框架内部时,它应该向上滚动页面,然后一旦加载就将iframe放回到它的预期位置。

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.

当然,在不知道更多的情况下,很难确定哪些权衡是可以的,而且这两个选项的条件都很有条件,所以YMMV。

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.

我希望有所帮助:)

这篇关于页面底部的iframe:避免自动滚动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆