解決iframe高度自適應的問題
最近做某項目,要用到iframe實現類似tab切換的功能,遇到高度不能自適應的問題,最后還是靠張鑫旭老師的文章解決問題,想看具體的直接點鏈接,下面上代碼:
父頁面代碼
var iframe = document.getElementById("iframe");
var iframeHeight = function() {
var hash = window.location.hash.slice(1),h;
if (hash && /height=/.test(hash)) {
h = hash.replace("height=", "");
iframe.height = h;
}
setTimeout(iframeHeight, 100);
};
iframeHeight();
子頁面代碼
var height = $(document).height();
var hostUrl = window.top.location.toString().split("#")[0];
if (hostUrl) {
hostUrl += "#height=" + height;
window.top.location = hostUrl;
}