問(wèn)題描述
使用Webview展示本地html數(shù)據(jù),偶現(xiàn)頁(yè)面空白
問(wèn)題分析
inspect頁(yè)面發(fā)現(xiàn)fontsize=0,查看前端代碼發(fā)現(xiàn)有設(shè)置fontsize的代碼
<script>
document.documentElement.style.fontSize = document.documentElement.clientWidth / 7.5 + "px";
</script>
Android的Webview由于自身機(jī)制問(wèn)題,此時(shí)獲取到的clientWidth可能不準(zhǔn)確,獲取到的clientWidth為0,導(dǎo)致頁(yè)面fontSize被設(shè)置成了0,表現(xiàn)為頁(yè)面空白。
問(wèn)題解決
在onload中重新渲染一次,表現(xiàn)正常。
<script>
document.documentElement.style.fontSize = document.documentElement.clientWidth / 7.5 + "px";
window.onload = function() {
document.documentElement.style.fontSize = document.documentElement.clientWidth / 7.5 + "px";
}
</script>