如何获取iframe的scrollTop
当window是iframe时,jQuery的scrollTop返回null。有谁能弄清楚如何获取 iframe 的scrollTop 吗?
更多信息:
我的脚本在 iframe 本身中运行,父窗口位于另一个域上,因此我无法访问 iframe 的 ID 或类似的内容
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我在尝试在 iframe 内设置scrollTop 时发现了这个问题...不完全是你的问题(你想要获取),但这里有一个 iframe 内的解决方案,供那些最终也在这里尝试设置的人使用。
如果您想滚动到页面顶部,在 iframe 内这将不起作用:
但是,这将起作用:
或者与 jQuery 等效:
对于此标记(包含在 iframe 内):
I found this question while trying to SET scrollTop inside an iframe... not exactly your question (you wanted to GET) but here's a solution inside iframes for people that also end up here trying to SET.
If you want to scroll to the top of the page this will NOT work inside an iframe:
However, this will work:
Or the equivilent with jQuery:
For this markup (contained inside an iframe):
您可以使用以下设置来设置
scrollTop
:所以您可以尝试像这样获取它:
因为不同的浏览器在不同的元素(body 或 html)上设置
scrollTop
。来自scrollTo插件:
但这在某些浏览器中可能仍然会失败。以下是Ariel Flesher 的 jQuery 滚动To 插件源代码中的相关部分:
然后您可以运行:
确定 iframe 向下滚动的距离。
You can set
scrollTop
by using this setup:So you could try getting it like this:
Because different browsers set the
scrollTop
on different elements (body or html).From the scrollTo plugin:
But that will probably still fail in certain browsers. Here is the relevant section from the source code of Ariel Flesher's scrollTo plugin for jQuery:
You may then run:
To determine how far the iframe has scrolled down.
好旧的 JavaScript:
Good old javascript:
仅适用于“0”作为参数
Only works with "0" as parameter
我尝试过
第
一个功能在桌面浏览器中成功运行。无法在移动浏览器中工作。所以我
在 scrollIntoView() 视图 的另一个函数“nofollow noreferrer”>scrollIntoView - MDN
I have try to
and
the first function work success in desktop browser. not working in mobile browser. so I use another function with scrollIntoView
scrollIntoView()
view in scrollIntoView - MDN我在 iframe 中也遇到了同样的问题。我的整个网站出现白色 iframe 并且滚动不起作用
所以我使用了这段代码并且工作正常。
第 1 步。将此代码放入您的 iframe
第 2 步。将此代码放入您调用 iframe 的位置
I have faced the same problem in my iframe. My whole website comming white iframe and scrolling not working
So I used this code and it's work fine.
Step 1. put this code to your iframe
Step 2. put this code where your call you iframe
或使用这个
or use this one
我知道我在这里玩游戏迟到了,但我试图在移动设备上解决这个问题。
由于跨域冲突(并且我无权访问父窗口),scrollTop() 对我不起作用,但更改高度确实如此:
I know I'm late to the game here, but I was trying to figure this out for a long list on mobile.
scrollTop() wasn't working for me due to cross-domain conflicts(and I didn't have access to parent window), but changing the height did:
因为您使用 iFrame,所以您需要将其与 Windows“消息”事件一起使用,
请阅读此处了解更多信息 信息
子视图(Iframe)
在家长视图中
**注意我使用“*”作为分隔符,你可以使用任何东西
+ 'form-iframe-top', "*");在家长视图中
**注意我使用“*”作为分隔符,你可以使用任何东西
Cause you using iFrame you will need to use it that way with windows "message" event
Read here for more Info
On Child View (Iframe)
On Parent View
**Note i used "*" as separator you can use anything
+ 'form-iframe-top', "*");On Parent View
**Note i used "*" as separator you can use anything
另一个提示:除了 this.myIframe.contentWindow.document.body.scrollTop 之外,您还可以从 addEventListener 中的事件获取scrollTop。然而,像“document.getElementById('iframe').scrollTop”之类的东西将始终不返回任何内容。
顺便说一句,我正在使用 React Native Web,这就是 ref 标签的来源
Another tip: You can also get scrollTop from the event in addEventListener in addition to this.myIframe.contentWindow.document.body.scrollTop. However, something like "document.getElementById('iframe').scrollTop" will always return nothing.
I'm using React Native Web by the way, that's where the ref tag comes from