CSS自适应分辨率 amfe-flexible 和 postcss-pxtorem:大屏高宽自适应问题

前言

继上篇《CSS自适应分辨率 amfe-flexible 和 postcss-pxtorem》
发现一个有趣的问题,文件 rem.js 中按照宽度设置自适应,适用于大多数页面,但当遇到大屏就不那么合适了。

问题

使用宽度,注意代码第2 和 4 行:

    // 1920 默认大小16px; 1920px = 120rem ;每个元素px基础上/16
    const screenWidth = 1920
    const scale = screenWidth / 16
    const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
    // 得到html的Dom元素
    const htmlDom = document.getElementsByTagName('html')[0]
    // 设置根元素字体大小
    htmlDom.style.fontSize = htmlWidth / scale + 'px'

如下图,左右卡片的底部因内容较多而溢出。这是按照宽度设置的,html font-size 较大。

,
解决方案:注意差异也在代码第 2和 4 行,此时已改为按照高度计算:

    // 按高度来
    const screenHeight = 1080
    const scale = screenHeight / 16
    const htmlHeight = document.documentElement.clientHeight || document.body.clientHeight
        // 得到html的Dom元素
    const htmlDom = document.getElementsByTagName('html')[0]
        // 设置根元素字体大小
    htmlDom.style.fontSize = htmlHeight / scale + 'px'

在这里插入图片描述

总结

关键点就在最后的值 htmlDom.style.fontSize。它决定了rempx 的转换。需根据系统的设计来做相应处理。有时甲方需要在异屏(2880*1800)系统上展示,也可固定 htmlDom.style.fontSize 的值,如直接赋值为 16px

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-21 10:38:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-21 10:38:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-21 10:38:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-21 10:38:03       20 阅读

热门阅读

  1. 使用Spark GraphX进行图形处理的代码练习

    2023-12-21 10:38:03       41 阅读
  2. 大数据学习(29)-spark on yarn底层原理

    2023-12-21 10:38:03       40 阅读
  3. 什么是类加载器,类加载器有哪些

    2023-12-21 10:38:03       33 阅读
  4. sklearn和tensorflow的理解

    2023-12-21 10:38:03       33 阅读
  5. 51单片机控制1602LCD输出整数和浮点数

    2023-12-21 10:38:03       40 阅读
  6. Linux 文本处理

    2023-12-21 10:38:03       39 阅读
  7. SQLite数据库的增删改查

    2023-12-21 10:38:03       40 阅读
  8. work随笔记录(实时更新)

    2023-12-21 10:38:03       45 阅读
  9. C语言第五十四弹---模拟使用strstr函数

    2023-12-21 10:38:03       33 阅读
  10. Avalonia MVVM

    2023-12-21 10:38:03       33 阅读
  11. 栈(C++)

    2023-12-21 10:38:03       29 阅读
  12. vim的使用

    2023-12-21 10:38:03       36 阅读
  13. 服务器不稳定因素

    2023-12-21 10:38:03       34 阅读