在 iFrame 内滚动
这是交易: 我花了 5 个小时为我的问题寻找一个好的解决方案,并发现了很多(不幸的是不起作用,或者是我做错了什么)。
我有一个页面,其中有一个 iFrame。 我不想滚动页面才能正确查看 iFrame。 我的观点是滚动该页面中的 iFrame,这样它就会改变它的位置(iFrame 内的页面)。
什么不起作用:将窗口scrollTo放入iFrame页面代码中(只是因为这个页面不是我的,所以即:我有自己的页面,在iFrame中我有stackoverflow.com,我希望它滚动100px到右侧和底部 500 像素)。
我希望你明白我的意思,并帮助我解决这个问题。
请善意地给我一个 iFrame 的完整工作示例。我只是无法忍受再花时间去弄清楚......这可能吗? ...
Here's the deal:
I spent 5 hours looking for a good solution for my problem and found many (unfortunately not working, or it was me doing sth wrong).
I have a page, in which I have an iFrame.
I do not want to scroll the page to see the iFrame properly.
My point is to scroll the iFrame in that page so it would change it's position (page inside iFrame).
What won't work: putting a window scrollTo into a iFrame page code (simply because this page is not mine, so i.e.: I have my own page and in iFrame I have stackoverflow.com, and I want it to scroll 100px to the right and 500px to the bottom).
I hope you got my point, and will help me to fix it.
Just please, be kind and give me the whole working example with an iFrame. I just can't stand spending any more time on figuring it out... Is it even possible? ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
佩卡是对的——这是不可能的。由于 iframe 的 src 来自不同的域,因此浏览器不会让父窗口中的代码访问 DOM 或窗口。这是重要且有意为之的。如果页面来自不同的域,则在单独窗口中运行的恶意脚本完全有可能导致父窗口出现严重问题。
Pekka is correct - it's not possible. Because the iframe's
src
is from a different domain, no browser will let code from the parent window access the DOM or window. This is important and intentional. If pages are from different domains, it's entirely possible that a malicious script running in a separate window could cause serious problems in the parent window.一种方法如下:
div
大小应小于iframe
大小。然后,您可以使用document.getElementById('fakeiframe').scrollTo(x,y);
滚动“iframe”。您可以修改 CSS 以根据需要调整框架。
One way to do it is as follows:
The
div
size should be smaller than theiframe
size. Then, you can scroll the "iframe" usingdocument.getElementById('fakeiframe').scrollTo(x,y);
You can modify the CSS to tweak the frame to your needs.