如何裁剪Iframe的内容以显示页面的一部分?

22

我目前正在开发一个网站,需要通过使用iframe从另一个网站下载内容。有没有办法裁剪已下载页面的内容,只在我的网站上显示页面的某个部分?

6个回答

49

有没有办法在我的网站上仅显示下载页面的某个部分?

不行。同源策略阻止您以任何方式操作iframe,包括滚动位置。

可以通过将iframe放入具有定义的高度和宽度以及overflow:hiddendiv容器中来实现绕过此限制,以剪切视口:

<div style="width: 500px; height: 500px; overflow: hidden">

并给 iframe 设置相对定位:

<iframe src="..." style="position: relative; left: -100px; top: -100px">

通过这种方式,您可以调整在页面上可见的iframe部分。但是,整个页面仍将被渲染,并且此方法不免受像iframe内滚动等影响。


谢谢!正是我所需要的。 - Arthur Frankel

7
<div style="position: absolute; left: 0px; top: 0px; border: solid 2px #555; width:594px; height:332px;">
<div style="overflow: hidden; margin-top: -100px; margin-left: -25px;">
</div>
<iframe src="http://www.centricle.com/tools/html-entities/" scrolling="no" style="height: 490px; border: 0px none; width: 619px; margin-top: -60px; margin-left: -24px; ">
</iframe>
</div>
</div>

这段代码对我有用。

来源 [ http://extechbuzz.blogspot.com/2012/12/iframe-how-to-display-specific-part-of.html ]


第三行多了一个<div>。 - Logan Cundiff

5
为了裁剪嵌入页面底部的广告,我使用了以下方法:

页面


<div style="width: 1000px; height: 390px; overflow: hidden">
<iframe src="https://openflights.org/user/dankohn1" width="1000"
height="600" style="border:none" scrolling="no">
</iframe></div>

1

0

这段代码对我有效...将margin-top和margin-left调整为最适合您的值,并将iframe的高度和宽度调整为您想要的任何值。

<html>
<div style="overflow: hidden; margin-top: -1440px; margin-left:0;">
<iframe src="https://fiitjeelogin.com/StartPage.aspx" scrolling="no" height="1550"width="300" frameBorder="0">
</iframe>
</div>
</html>

0
我成功地阻止了页面滚动。 只需添加:scrolling="no"
<div class="form-computer">
    <iframe src="https://docs.google.com/forms/d/e/1FAIpQLSf_rtGkQkzqHEw1IBk53QDITiSWDYW5RJQKDUvojAGN5dIXsg/viewform?embedded=true"
        width="100%" height="1915px" style="position: relative; top: -135px"
        frameborder="0"
        marginheight="0"
        marginwidth="0"
        scrolling="no"
        >Social Media Use
    </iframe>
  </div>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接