1、如果被嵌入页面和当前页面不跨域,可以使用Iframe的@load加载事件获取内部高度
<iframe name="outerPage" @load="iframeload()" :src="url" ref="iframe"
scrolling="auto" frameborder="0" id="outerPage"
:style="{ 'min-width': '100%', 'min-height': '100%' }" ></iframe>
iframeload() {
setTimeout(() => {
try {
const cIframe = document.getElementById("outerPage");
let aWindow = cIframe.contentWindow;
let aWindowHeight =
aWindow .document.documentElement.scrollHeight ||
aWindow .document.body.scrollHeight;
let doc = cIframe.contentDocument || cIframe.document;
let cHeight = Math.max(
doc.body.clientHeight,
doc.documentElement.clientHeight
);
let sHeight = Math.max(
doc.body. scrollHeight,
doc.documentElement.scrollHeight
);
let lheight = Math.max(cHeight, sHeight);
let finalHeight = Math.max(lheight, aWindowHeight );
this.iframeHeight = finalHeight + "px";
} catch (e) {
//跨域获取不到
throw new Error("自定义错误setIframeHeight Error");
}
}, 1500);
},
获取改变iframe高度
最新推荐文章于 2022-08-31 16:19:55 发布