首页 > 常识 > 互联网 > 网站制作 > 基础与技巧

关于IFRAME自适应高度的研究

作者:  时间:2016-06-02
描述:本文详细介绍了iframe框架高度随内容的变化而变化的代码。
《生活宝典shenghuobaodian.com》讯  本文详细介绍了iframe框架高度随内容的变化而变化的代码。
 
重要提示:src=中你必须填写的网页地址,一定要和本页面在同一个站点上,否则,会抱错,说“拒绝访问!”

之前自己也碰到过这个问题,为了得到答案去网上搜索,发现有不少人也遇到了这样的问题,现在就把解决方法共享一下

超简单哦

1、建立一个bottom.js的文件,然后输入下面的代码(只有两行哦)

parent.document.all("框架ID名").style.height=document.body.scrollHeight;

parent.document.all("框架ID名").style.width=document.body.scrollWidth;

这里的 框架ID名 就是Iframe的ID,比如:

<IFRAME id="框架ID名" name="left" frameBorder=0 scrolling=no src="XXX.asp" width="100%"></IFRAME>

2、给你网站里所有的被包含文件里面每个都加入

<script language = "JavaScript" src = "bottom.js"/></script>

iframe的高度随内容的变化而变化

正文: 
本文详细介绍了iframe框架高度随内容的变化而变化的代码。

<iframe src="XXX.htm" width="700px" frameborder="0" id="frmsrc" scrolling="no" marginheight="0" onl oad="AutoIframe()"></iframe>

<script type="text/javascript">
function AutoIframe()
{
if(document.readyState!='complete')
{
setTimeout( function(){AutoIframe();},25 );
return;
}
else
{
var ifobj=document.getElementById("frmsrc");
ifobj.height= ifobj.contentWindow.document.body.scrollHeight;
}
}
</script>

//PS: XXX.htm不可跨域

<iframe src="dd.html" id="frmMain" name="frmMain" width="99%" height="100%" frameborder=0 scrolling=no marginHeight=0 marginWidth=0 onl oad="Javascript:SetWinHeight(this)" ></iframe>

function SetWinHeight(obj)
{
var win=obj;
if (document.getElementById)
{
if (win && !window.opera)
{
if (win.contentDocument && win.contentDocument.body.offsetHeight)
win.height = win.contentDocument.body.offsetHeight;
else if(win.Document && win.Document.body.scrollHeight)
win.height = win.Document.body.scrollHeight;
}
}
}

用iframe自动适应高度2

很多做网站的人都用过IFrame,也常希望能让IFrame自动改变大小,以美化全局效果。以下是两种方法 
方法一:(较通用) 
<script language="Javascript">
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0

function dyniframesize(iframename) {
var pTar = null;
if (document.getElementById){
pTar = document.getElementById(iframename);
}
else{
eval('pTar = ' + iframename + ';');
}
if (pTar && !window.opera){
//begin resizing iframe
pTar.style.display="block"

if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
//ns6 syntax
pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight; 
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
}
}
}
</script>

<iframe id="myTestFrameID" 
onload="javascript:{dyniframesize('myTestFrameID');}" 
marginwidth=0 marginheight=0 frameborder=0 
scrolling=no src="/myiframesrc.php" 
width=200 height=100></iframe><script language="Javascript">
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0

function dyniframesize(iframename) {
var pTar = null;
if (document.getElementById){
pTar = document.getElementById(iframename);
}
else{
eval('pTar = ' + iframename + ';');
}
if (pTar && !window.opera){
//begin resizing iframe
pTar.style.display="block"

if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
//ns6 syntax
pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight; 
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
}
}
}
</script>

<iframe id="myTestFrameID" 
onload="javascript:{dyniframesize('myTestFrameID');}" 
marginwidth=0 marginheight=0 frameborder=0 
scrolling=no src="/myiframesrc.php" 
width=200 height=100></iframe>
方法二:(较简便) 
<IFRAME id=headlogin marginWidth=0 marginHeight=0 src="xxx" frameBorder=0 width=100% scrolling=no height=25 onl oad="this.height=this.contentWindow.document.body.scrollHeight"></IFRAME>

从手机浏览器访问《生活宝典》

站内搜索
无相关信息
微商帮
世界网站大全
中国科研团队:大象的长鼻子是如何越变越长了
中国科研团队:大象的长
自己是否能够像自己想的那样做成一件事,身旁人的判断会比自己准确
自己是否能够像自己想
火宫殿:吃喝玩乐看,烧香拜神佛,红火的庙会经济
火宫殿:吃喝玩乐看,烧香
眼见为实也不为实,更须谨慎看到的并非全貌
眼见为实也不为实,更须