CSS完美实现iframe高度自适应(支持跨域)

代码强大之处:

1. 该方法完美兼容 IE6,7,8 ,Fire fox,chrome,opera 等主流的浏览器;

2.同域,跨域皆支持;

3.不调用任何 JS 脚本;

注意三点

1. 文件开头不能是:必须 是开头

2. body 样式中的 overflow: hidden; 绝对不对省略;

3.Iframe 中的 height='100%' 以及 滚动条不能设为 no(默认是 yes,不用设置即可)

<script>

// 计算页面的实际高度,iframe自适应会用到

function calcPageHeight(doc) {

var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)

var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)

var height = Math.max(cHeight, sHeight)

return height

}

//根据ID获取iframe对象

var ifr = document.getElementById('main')

ifr.onload = function() {

//解决打开高度太高的页面后再打开高度较小页面滚动条不收缩

ifr.style.height='0px';

var iDoc = ifr.contentDocument || ifr.document

var height = calcPageHeight(iDoc)

if(height < 850){

height = 850;

}

ifr.style.height = height + 'px'

</script>

原文地址https://www.22vd.com/34820.html

原文地址:https://www.cnblogs.com/changeMe/p/10916955.html