现在开发的一个移动端H5页面,内嵌了iframe展示内容。在部分iOS系统(iOS 12)下,遇到了一些问题:iframe不受宽高样式的限制,加载后,显示完整高度,于是需要用iframe区域内部滚动无法做到。
调整方案为使用div包裹iframe,通过div的滚动来实现iframe的滚动。
在iOS下,div的滚动,仅设置overflow-y:scroll 不行,还需要加一行样式-webkit-overflow-scrolling: touch;
以下是伪代码:
<div class="iframe-box" @scroll="boxScroll"> <iframe :src="url" class="iframe" @load="iframeLoad"></iframe> </div>
const iframeLoad = () => { const iframe = document.querySelector('.iframe') const iframeWindow = iframe.contentWindow console.log(iframeWindow.document.documentElement.scrollHeight) iframe.style.height = iframeWindow.document.documentElement.scrollHeight + 'px'}
function boxScroll(){
...
}
.iframe-box { flex: 1; -webkit-overflow-scrolling: touch; overflow-y: scroll;}.iframe { width: 100%; border: 0;}