展开目录
华为等国产手机rem宽度超过实际宽度
兼容rem移动端
X
陈尼玛的博客
记录开发生涯的踩坑经历,用时间来验证成长
加载中

移动端用rem布局设置根元素字号:

document.documentElement.style.fontSize=innerWidth/10+'px'

即1rem=10%的屏幕宽度,在大部分手机上是没问题的,但部分国产机这种方式设置的值会偏大,例如华为某型号宽度为360px,而10rem等于414px。

因此需要加一个缩放参数,在设置字号时乘上这个参数。

const remZoom=(_=>{
  try{
    const docEl=doc.documentElement
    const div=document.createElement('div')
    div.style.width='10rem'
    div.style.position='absolute'
    div.style.top='-100rem'
    div.style.height='1rem'
    document.body.appendChild(div)
    docEl.style.fontSize=(docEl.clientWidth/10)+'px'
    return docEl.clientWidth/div.offsetWidth
  }catch(e) {}
  return 1
})()

document.documentElement.style.fontSize=innerWidth/10*remZoom+'px'

相关文档

暂无

随便看看

  1. windows电脑防止自动休眠

  2. cnpm 立即同步

  3. css3自定义滚动条样式

  4. react项目webpack打包时拆分异步加载的文件

  5. sass变量和继承类写法

  6. nodejs 长连接

  7. npm远程服务器某些配置不兼容代理的解决办法

  8. SSL certificate problem: self signed certificate in certificate chain

  9. npm包命令行调用

  10. git删除远程分支

  11. python 用摄像头拍照并写入文件

  12. heroku查看app使用时间

  13. git配置服务端支持http认证

  14. git记住/删除账号密码

  15. ssl 证书生成方式

  16. mysql选取内容导出到文件

  17. ie8上Image.onload不触发问题

  18. 数据库清理优化

  19. 前端性能观察器

畅言模块加载中