无论iframe中加载的是什么内容,都可以让iframe的高度和内容保持一致,这样就可以不让iframe出现垂直滚动条了
<template>
<iframe id="ifra" src="xxx" frameborder="0" class="iframe"></iframe>
</template>
<script>
methods: {
// 这里的viewer是iframe中的子元素,实际开发中我这里引入了pdfjs用来进行pdf在线展示,所以需要获取viewer的高度
setIframeHeight(iframe) {
let iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow
if (iframeWin.document.getElementById('viewer')) {
iframe.height = iframeWin.document.getElementById('viewer').scrollHeight
}
}
},
mounted() {
let ths = this
// 需要在iframe加载完成后调用该方法,或者在$nextTick中进行方法调用,看实际情况
setTimeout(function() {
ths.setIframeHeight(document.getElementById('ifra'))
}, 1000)
}
</script>