1.解決iframe顯示高度自適應問題 css
2.解決在chrome下ifame跳轉高度不能減小 chrome
3.解決IE Chrome兼容問題(其它瀏覽器未測試) 瀏覽器
HTML: 測試
<iframe frameborder="0" scrolling="no" id="iframe" onload="reinitIframeChrome(this)" onreadystatechange="reinitIframeIE(this)"> this
onreadystatechange這個方法只在IE下適用,Chrome下不會被調用 spa
Javascript: ip
//iframe高度自適應
function reinitIframeIE(frame) {
if(frame.readyState=="complete"){//待頁面加載完成後調整高度
changeHeight();
}
}
function reinitIframeChrome(frame) {
if(document.readyState!="loading"){//待頁面加載完成後調整高度
changeHeight();
}
}
function changeHeight(){
var iframeid = document.getElementById("iframe"); // iframe id
iframeid.height = "642px";// 先給一個夠小的初值,而後再長高.
iframeid.style.height ="642px";//css也要行被縮小,不然跳轉時滾軸長度不會變
if (document.getElementById) {
if (iframeid && !window.opera) {
if (iframeid.contentDocument
&& iframeid.contentDocument.body.offsetHeight) {
iframeid.height = iframeid.contentDocument.body.offsetHeight;
} else if (iframeid.Document && iframeid.Document.body.scrollHeight) {
iframeid.height = iframeid.Document.body.scrollHeight;
}
}
}
iframeid.style.height = iframeid.height+"px";//只有調整css的height後,才被在外圍窗口顯示滾軸
} get