網站首頁 編程語言 正文
一、下載postcss插件適配rem
1.0 安裝依賴
在文件目錄下cmd到黑窗戶 運行以下代碼
npm install amfe-flexible -S
npm install postcss-px2rem -S
1.1 引入lib-flexible
// 在入口main.js中 引入 lib-flexible
import "amfe-flexible/index.js";
注意事項(important): 由于flexible會動態給頁面header中添加標簽,所以務必請把目錄 public/index.html 中的這個標簽刪除!!!
1.2 PostCSS 配置
新建postcss-pxtorem 或者在postcss.config.js中都能配置
// https://github.com/michael-ciniawsky/postcss-load-config
module.exports = {
plugins: {
autoprefixer: {
overrideBrowserslist: ['Android 4.1', 'iOS 7.1', 'Chrome > 31', 'ff > 31', 'ie >= 8']
},
'postcss-pxtorem': {
rootValue: 37.5,
propList: ['*']
}
}
}
1.3 使用方法
上面的步驟寫完后,使用時設計圖750px的,那設計圖要是375px的寬度,那代碼里面寫成375px。因為vant中的寬度是雙面屏,所以寬度需要減半。
二、 手寫rem
style
<style>
* {
margin: 0;
padding: 0;
}
.h {
width: 7.5rem;
height: 0.8rem;
background-color: red;
}
.c {
width: 7.5rem;
background-color: orange;
height: 5rem;
}
.f {
width: 7.5rem;
background-color: orchid;
height: 0.5rem;
}
</style>
body
<body>
<div class="h"></div>
<div class="c"></div>
<div class="f"></div>
</body>
script
<script>
// 封裝一個根據屏幕尺寸自動改變html的font-size大小的函數
function rem() {
// 獲取html的寬度
let htmlW = document.documentElement.clientWidth;
// 獲取body的寬度
let bodyW = document.body.clientWidth;
// IE10以下獲取不到HTML的寬度 兼容處理
let W = htmlW || bodyW;
// 設置html的font-size大小
// 因為設計圖尺寸是750,所以平分,這樣*100之后,1rem就等于100px;
document.documentElement.style.fontSize = (W / 750 * 100) + 'px';
}
// 定義屏幕寬度改變時觸發
window.onresize = function () {
rem()
}
// 設置初始觸發一次,刷新也都會觸發
rem()
</script>
原文鏈接:https://blog.csdn.net/weixin_58102387/article/details/122294200
相關推薦
- 2022-07-25 Python文件操作實戰案例之用戶登錄_python
- 2022-12-27 python利用logging模塊實現根據日志級別打印不同顏色日志的代碼案例_python
- 2023-04-26 numpy數組切片的使用_python
- 2022-06-12 C語言?智能指針?shared_ptr?和?weak_ptr_C 語言
- 2022-08-16 python+pytest接口自動化參數關聯_python
- 2022-11-07 python學習pymongo模塊的使用方法_python
- 2022-03-19 C語言完全平方整數的判斷_C 語言
- 2022-07-01 詳解如何在Go語言中調用C源代碼_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支