1、iframe自适应页面高度
首先需要给iframe设置一个id,不需要滚动条则加上scrolling="no"
然后加上一个onload事件
function iFrameHeight(iframe) {
var ifm= document.getElementById(iframe.id);
var subWeb = document.frames ? document.frames[iframe.id].document : ifm.contentDocument;
if(ifm != null && subWeb != null) {
ifm.height = subWeb.body.scrollHeight;
ifm.width = subWeb.body.scrollWidth;
}
}
<iframe src=' ' width='100%' id="compInfo" frameborder='0'scrolling="no" onload="iFrameHeight(this)"></iframe>
2、若需要iframe固定一个高度,超过这个高度才自适应
function iFrameHeightContact(iframe) {
var ifm= document.getElementById(iframe.id);
var subWeb = document.frames ? document.frames[iframe.id].document : ifm.contentDocument;
if(ifm != null && subWeb != null) {
var ifmHeight = subWeb.body.scrollHeight;
var ifmWidth = subWeb.body.scrollWidth;
if(ifmHeight<400){
ifm.height = 400;
ifm.width = ifmWidth;
} else {
ifm.height = ifmHeight;
ifm.width = ifmWidth;
}
}
}
<iframe src=' ' width='100%' id="compInfo" frameborder='0'scrolling="no" onload="iFrameHeight(this)"></iframe>
* 400则为你想要固定的高度