【问题标题】:positioning content of an iframe within a containing div在包含的 div 中定位 iframe 的内容
【发布时间】:2015-03-25 04:18:46
【问题描述】:

我有一个页面需要通过 iframe 提供服务,但我只需要显示页面的一部分,而不是整个页面。

我当前的代码是:

<div style="display:block;overflow:hidden;width:500px;height:350px;">
<iframe height="350px" scrolling="no"
src="http://my/page/i/want/to/show/part/of/here/" width="500px"></iframe></div>

我认为最好的选择是在包含“溢出:隐藏”的 div 中提供 iframe,以便它像相框一样工作。这行得通,但问题是我要显示的内容位于 iframe 页面的中间,而 div 总是假设 0,0 是框架的开始。我需要定位 iframe,以便 div 正好位于我想要显示的页面部分。

我可以这样做吗?

【问题讨论】:

标签: css iframe positioning


【解决方案1】:

对 margin-top 和 margin-left 使用负值来定位 iframe。看下面的代码:

<div style="display:block;overflow:hidden;width:500px;height:350px;">
<iframe style="margin-top:-100px;margin-left:-100px" height="350px" scrolling="no"
src="http://my/page/i/want/to/show/part/of/here/" width="500px"></iframe></div>

【讨论】:

    【解决方案2】:

    在 iframe 中显示的内容中,如果您可以设置一个带有 id 的元素,该 id 标记您想要通过的内容部分的最顶部,那么您可以使用该锚点设置 iframe 的 src 属性网址

    iframe 内容的 HTML:

    [a bunch of markup/stuff that you don't want to show]
    ....
    <div id="topOfVisibleArea"></div>
    
    [more markup]
    

    iframe 标签:

    <iframe height="350px" scrolling="no" 
    src="http://my/page/i/want/to/show/part/of/here/#topOfVisibleArea" 
    width="500px"></iframe>
    

    更新——更好的方法:

    或者您可以在 div 内的 iframe 上使用绝对定位。您需要 iframe 的高度和宽度比您安装它的窗口更宽更高,以适应偏移量。

    请参阅此示例:http://jsfiddle.net/sNSMw/

    【讨论】:

    • 啊,好点,只是我无法控制 iframe 中的页面来源。所以任何调整都必须在初始页面上进行。
    • 为什么内容的位置在 Firefox 中比在 Chrome 中低?如果您比较它,您会发现它们显示的内容并不完全相同。这可以解决吗?
    【解决方案3】:
    <iframe name="itunes" src="http://itunes.apple.com/us/album/threads/id509846713&quot; frameborder="0" width="500" height="600" onload="window.frames['itunes'].scrollTo(250,250)"></iframe>
    

    【讨论】:

    • 当 iframe 包含外部网页时不起作用。该脚本被浏览器阻止。
    【解决方案4】:

    Trick 全部在 iframe 样式参数中。放置在额外的容器中将有助于满足对齐要求。

    <div style="border: 1px solid red; width: 70px; height: 20px; overflow:  
    hidden;"><iframe style="width: 400px; height: 800px; margin-top: -200px; 
    margin-left: -200px;" src="http://www.amazon.co.uk/product-reviews
    /B0051QVF7A/ref=cm_cr_dp_see_all_top?ie=UTF8&showViewpoints=1&
    sortBy=bySubmissionDateDescending" width="320" height="240"></iframe>
    </div>
    

    感谢 spamtech.co.uk 的帮助和示例:http://spamtech.co.uk/tips/position-content-inside-an-iframe/

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      相关资源
      最近更新 更多