当前位置:网站首页>rem适配
rem适配
2022-08-02 03:07:00 【小叶_01】
- 为什么做 rem 适配
a) 机型太多,不同的机型屏幕大小不一样
b) 需求: 一套设计稿的内容在不同的机型上呈现的效果一致,根据屏幕大小不同的变化,页面中的内容也相应变化 - rem和em区别
rem: (root em),rem是css中的一种相对长度单位。
相对于根元素(即 html元素)font-size计算值的倍数
em是相对于父标签元素 - 实现1:
function remRefresh() {
let clientWidth = document.documentElement.clientWidth; // 将屏幕等分 10 份
let rem = clientWidth / 10;
//document.documentElement即是html
document.documentElement.style.fontSize = rem + 'px';
document.body.style.fontSize = '12px';
}
window.addEventListener('pageshow', () => {
remRefresh()
})
// 函数防抖
let timeoutId;
window.addEventListener('resize', () => {
timeoutId && clearTimeout(timeoutId);
timeoutId = setTimeout(() =>{
remRefresh()
}, 300)
})
补充也可以在html文件中配合媒体查询设计html元素
@media screen and (min-width: 320px) {
html {
font-size: 21.33px;
}
}
@media screen and (min-width: 750px) {
html {
font-size: 50px;
}
}
- 实现2:
第三方库实现lib-flexible + px2rem-loader等其他库
边栏推荐
- WebShell连接工具(中国菜刀、WeBaCoo、Weevely)使用
- MySQL修改最大连接数限制
- Foundry教程:使用多种方式编写可升级的智能代理合约(下)
- MySQL函数(经典收藏)
- MySQL8.0.26 installation and configuration tutorial (windows 64-bit)
- IPIDEA的使用方式
- 咨询cdc for oracle,增量同步scan.startup.mode只有initial和la
- 【LeetCode】1374. Generate a string with an odd number of each character
- 利用WebShell拿Shell技巧
- Heao Technology Network Interview (with reference answers)
猜你喜欢
随机推荐
IPFS deployment and file upload (golang)
ModuleNotFoundError: No module named ‘openpyxl‘
请教各位大佬,如果我代码里面设置了,这个id我在什么地方可以查到呢?连接到mysql cluste
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
Nacos source code analysis topic (2) - service registration
PyTorch(六)——PyTorch可视化
有人知道HTML怎么到MYSQL数据库吗? (NODEJS)
VPS8505 微功率隔离电源隔离芯片 2.3-6V IN /24V/1A 功率管
7-35 城市间紧急救援 (25 分)c语言(测试点二未通过)
* Compare version numbers
MySQL8 -- use msi (graphical user interface) under Windows installation method
5.合宙Air32F103_LCD_key
WebShell特征值汇总与检测工具
ROS2自学笔记:launch文件完整编写流程
WebShell connection tools (Chinese kitchen knife, WeBaCoo, Weevely) use
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
PHP WebSehll backdoor script and detection tool
PHP WebShell 免杀
【LeetCode】20. Valid parentheses
Chapter 10_Index Optimization and Query Optimization









