`
齐晓威_518
  • 浏览: 605964 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

网页制作中Iframe自适应高度的解决办法

 
阅读更多
方法:在主页面Iframe的onload事件中执行JS,去取得被包含页的高度,然后去同步高度。并在主窗口做一个Interval,不停的来获取被包含页的高度。当Iframe窗体高度高于文档实际高度的时候,高度取的是窗体高度,而当窗体高度低于实际文档高度时,取的是文档实际高度。因此,要想办法在同步高度之前把高度设置到一个比实际文档低的值。所以,添加 onload=”this.height=100″,让页面加载的时候先缩到足够矮,然后再同步到一样的高度。

    代码如下所示:

<Iframe src="cs2-j.html" name="contain" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" id="contain" onload="this.height=100" style=" WIDTH: 100%; "></Iframe>

// height=100这个数值可以根据需要自行设置,即页面加载时显示的最小高度。

<script type="text/javascript">

function reinitIframe(){

var Iframe = document.getElementById("contain");

try{

// 声明变量取值

var bHeight = Iframe.contentWindow.document.body.scrollHeight;

vardHeight=Iframe.contentWindow.document.documentElement.scrollHeight;

var height = Math.max(bHeight, dHeight); // 取最大值

Iframe.height = height;

}catch (ex){}

}

window.setInterval("reinitIframe()", 200);

</script>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics