代碼強大之處:
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>