如何让iframe自适应页面大小(有码)

kk706 2010-12-30 10:22:38
有一个页面,这个页面就嵌入了一个iframe,没有其他内容,如下:

<form id="form1" runat="server">
<iframe id="fid" name="fname" src=""></iframe>
</form>


页面加载时,在pageload方法中获得 iframe的src,然后将获得src值付给iframe。

现在的问题是,如何让iframe自适应页面大小,我设置了iframe的width 和 height 为100%,但是右侧页面出现了两个滚动条,一个aspx页面的,一个iframe里面的,请问如何设置,才能让人感觉不到iframe的存在呢?

谢谢。
...全文
1706 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
缪军 2010-12-30
  • 打赏
  • 举报
回复
注意:不要设置iframe的高度
缪军 2010-12-30
  • 打赏
  • 举报
回复

protected void Page_Load(object sender, EventArgs e){
this.fid.Attributes["onload"] = "this.height=fid.document.body.scrollHeight + 50";
}
PitTroll 2010-12-30
  • 打赏
  • 举报
回复
马赛克。
laowang134 2010-12-30
  • 打赏
  • 举报
回复

<script type="text/javascript">
function reinitIframe(){
var iframe = document.getElementById("mainFrame");//id改为你的iframe的id
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
</script>


-----
直接加进这段JS代码。。。其他什么都不用管。。
hanguoji84 2010-12-30
  • 打赏
  • 举报
回复
框架页如下写:
<iframe src="Index.aspx" align="middle" id="mainFrame" onload='this.height=document.frames["mainFrame"].document.body.scrollHeight'
hspace="0" vspace="0" WIDTH='100%' marginwidth="0" MARGINHEIGHT="0" FRAMEBORDER='0'
SCROLLING='no'></iframe>

框架内的页:
<script language="JavaScript">
function f_frameStyleResize()
{
var HeightValue = document.body.scrollHeight;
if(HeightValue < 500)
{
HeightValue = 500;
}
parent.document.getElementById("mainFrame").style.height = HeightValue;
}
hu0336 2010-12-30
  • 打赏
  • 举报
回复
<script language="JavaScript" type="text/JavaScript">
function autoResize()
{
try
{
document.all["iframe1"].style.height=iframe1.document.body.scrollHeight
}
catch(e){}
}
</script>
<IFRAME name=iframe1 id=iframe1 src="test.htm" frameborder="NO" border="0" framespacing="0" width="100%" style="height:expression(1);aho:expression(autoResize())"></IFRAME>

本文来自: 小胡设计室(http://www.xhsjs.com/), 详细出处参考:http://www.xhsjs.com/NET/ZhiShi/100000363.shtml
koukoujiayi 2010-12-30
  • 打赏
  • 举报
回复
自适应js,(ie,FF适用):
    <script type="text/javascript">
var FFextraHeight = 0;
if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
FFextraHeight = 16;
}
function ReSizeiFrame(obj) {
var iframe = document.getElementById(obj);
if (iframe && !window.opera) {
iframe.style.display = "block";
if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) {
iframe.height = iframe.contentDocument.body.offsetHeight + FFextraHeight;
}
else if (iframe.Document && iframe.Document.body.scrollHeight) {
iframe.height = iframe.Document.body.scrollHeight;
//alert(iframe.Document.body.scrollHeight);
}
}
}
</script>


调用:
<iframe id="fid" name="left" src="" scrolling="no" frameborder="0" onload="ReSizeiFrame('fid')"></iframe>
加油馒头 2010-12-30
  • 打赏
  • 举报
回复
<script>
//IFrame自适应高度
function reinitIframe(){
var iframe = document.getElementById("infoshowiframeid");//IFRAME id
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = Math.max(height,400);
// alert(height)
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);

</script>

刚刚也在研究的,试过了 兼容IE,FF

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧