`
shuai1234
  • 浏览: 995544 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

iframe 自适应高度,无限级父框架,多层框架自适应

阅读更多

 

问题描述
    今天在处理一个Iframe自适应高度时遇到一个多层Iframe引用时的高度不能撑开的问题,效果同Demo1.


原因分析
    问题是由引用的顺序引起的,我们假设有3层Iframe引用,h1.html,h2.html,h3.html  h1包含h2,h2又包含h3,h3.html中有内容会出现高度适应问题,一般的处理方式是我们在引用h3的iframe中加入onload事件来让这个Iframe高度自适应引用的页面高度。这时我们打开h1.html就开发页面看起还是没有撑开,这是因为页面是一级一级的加载下去的,当h1引用h2 时,h2的高度是固定的,当h3加载完毕的时候h2的高度做了变化,但是这时h1的引用页面高度并没有改变就造成了页面看起来还是没有撑开,经和同事讨论后编写了一个JS的自动重新计算parent(父页面)中Iframe元素的高度,从而解决这个问题,原理主要是通过一直循环获取页面parent的 Iframe元素,当获取到top层时就结束

 

单个iframe适应

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->onload="this.style.height=iframe1.document.body.scrollHeight"

注:iframe1为该框架的ID,我在尝试不硬写名字的自适应方法,如果成功就换上来。

 

自适应多级iframe高度

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->//进行Iframe的自动撑开,让所有父页面的Iframe都自动适应包含页高度 function autoHeight(){ var doc = document, p = window; while(p = p.parent){ var frames = p.frames, frame, i = 0; while(frame = frames[i++]){ if(frame.document == doc){ frame.frameElement.style.height = doc.body.scrollHeight; doc = p.document; break; } } if(p == top){ break; } } }

 

 

 

 

 

某网友评论:

帖一個我們之前用的方法吧,比較好用:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1] var FFextraHeight=getFFVersion>=0.1?16:0 function dyniframesize(iframename) { var pTar = null; if (document.getElementById){ pTar = parent.document.getElementById(iframename); } else{ eval('pTar = ' + iframename + ';'); } if (pTar && !window.opera){ if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){ pTar.style.height = pTar.contentDocument.body.offsetHeight+FFextraHeight+25; } else if (pTar.Document && pTar.Document.body.scrollHeight){ pTar.style.height = pTar.Document.body.scrollHeight; } } }
分享到:
评论

相关推荐

    多层嵌套iframe 自适应高度的解决方法

    多层嵌套iframe 自适应高度的解决方法

    iframe 多层嵌套 无限嵌套 高度自适应的解决方案

    然而,当`iframe`进行多层嵌套时,特别是无限嵌套,如何保证每个`iframe`的高度都能自适应其内容,避免滚动条的出现,就成为了一个挑战。本文将探讨一种解决方案,适用于`A`、`B`、`C`三个页面互相嵌套的情况。 ...

    iframe多层级自动适应高度

    本主题探讨的是如何处理`iframe`多层级时的高度自适应问题,以确保内容能够完整展示而不会造成滚动条混乱或内容被裁剪。 首先,我们要理解`iframe`的基本属性。`src`属性用于指定要加载的URL,`width`和`height`...

    iframe实用操作锦集

    iframe高度自适应于父页面 有时候我们需要让`iframe`的高度适应父页面的剩余空间,这可以通过监听窗口的`resize`事件实现: ```javascript $(window).resize(function () { var webheight = document.body....

    ASP.NET学习笔记

    - CSS Flexbox或Grid:在现代浏览器中,可以利用弹性布局或网格布局实现iframe的高度自适应。 这些知识点构成了ASP.NET开发的基础,熟练掌握它们能帮助开发者构建高效、安全且用户体验良好的Web应用。

    页面布局frameset frame

    它的主要属性有`rows`和`cols`,用于定义框架集的行数和列数,可以是像素值、百分比或者星号(*)表示自适应大小。 例如: ```html , 50%"&gt; ``` 在这个例子中,浏览器窗口被分为两半,上半部分显示`top.html`,...

    深入解析contentWindow, contentDocument

    首先,它获取了父级iframe的`contentWindow`,然后通过`contentWindow`进一步获取到子级和孙级iframe的`contentDocument`,计算出它们的内容高度,并将这些高度设置回对应的iframe元素,从而实现自适应高度的效果。...

Global site tag (gtag.js) - Google Analytics