網站首頁 編程語言 正文
開始前:一定要記住,在iphone5下,1rem=16px;
圖示:
下面開始三個步驟:
1.獲取html的寬
操作代碼:
let htmlwidth=document.documentElement.clientWidth || document.body.clientWidth;//有些瀏覽器documentElement獲取不到,那就使用后面的body
2.獲取htmlDom元素
let htmlDom=document.getElementByTagName('html')[0]
3.設置html樣式
htmlDom.style.fontSize=htmlwith/20+'px';
完整代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
.test{
width: 20rem;
height: 10rem;
background-color: skyblue;
text-align: center;
}
.hello{
color: red;
font-size: 1rem;
}
</style>
<body>
<div class="test">
<div class="hello">hello JSPang</div>
</div>
</body>
</html>
<script>
// 三個步驟:
// 1.獲取html的寬,
let htmlwidth=document.documentElement.clientWidth || document.body.clientWidth;//有些瀏覽器documentElement獲取不到,那就使用后面的body
console.log(htmlwidth);
// 2.htmlDom
let htmlDom=document.getElementsByTagName("html")[0]
console.log(htmlDom);
//3.設置根元素樣式
htmlDom.style.fontSize=htmlwidth/20+'px';//記住這個20是等份的意思,這樣每一份是16px,即1rem=16px;
</script>
效果:點擊不同的設備后刷新,都可以適配.
上面再進行優(yōu)化:添加監(jiān)聽事件,讓它自動監(jiān)聽窗口變化動態(tài)改變.
完整代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
.test{
width: 20rem;
height: 10rem;
background-color: skyblue;
text-align: center;
}
.hello{
color: red;
font-size: 1rem;
}
</style>
<body>
<div class="test">
<div class="hello">hello JSPang</div>
</div>
</body>
</html>
<script>
function gethtmlfontsize(){
// 三個步驟:
// 1.獲取html的寬,
let htmlwidth=document.documentElement.clientWidth || document.body.clientWidth;//有些瀏覽器documentElement獲取不到,那就使用后面的body
console.log(htmlwidth);
// 2.htmlDom
let htmlDom=document.getElementsByTagName("html")[0]
console.log(htmlDom);
//3.設置根元素樣式
htmlDom.style.fontSize=htmlwidth/20+'px';
}
// 調用一次
gethtmlfontsize();
// 添加監(jiān)聽事件(resize是監(jiān)聽的意思)
window.addEventListener('resize',gethtmlfontsize)
</script>
效果:
上面是參考iphone5的320寬的寫法,iphone678代碼如下:
<script>
/**
* ================================================
* 設置根元素font-size
* 當設備寬度為375(iPhone6)時,根元素font-size=16px;
× ================================================
*/
(function (doc, win) {
var docEl = win.document.documentElement;
var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
var refreshRem = function () {
var clientWidth = win.innerWidth
|| doc.documentElement.clientWidth
|| doc.body.clientWidth;
console.log(clientWidth)
if (!clientWidth) return;
var fz;
var width = clientWidth;
fz = 16 * width / 375;
docEl.style.fontSize = fz + 'px';//這樣每一份也是16px,即1rem=16px
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, refreshRem, false);
doc.addEventListener('DOMContentLoaded', refreshRem, false);
refreshRem();
})(document, window);
</script>
補充:禁止客戶雙指縮放辦法
最后附上一個更完美的解決方案(用別人寫好的hotcss.js文件,你就可以直接寫px像素,它會自動幫你轉成rem):
我的收藏的hotcss.js文件地址
hotcss.js文件拷貝到viewport.js里,然后在webpack.config.js里面引入即可.
原文鏈接:https://blog.csdn.net/xiaodi520520/article/details/90056865
相關推薦
- 2023-05-13 python中數字列表轉化為數字字符串的實例代碼_python
- 2022-06-16 C#中執(zhí)行SQL的幾種方法講解_C#教程
- 2022-09-15 windows中cmd下添加、刪除和修改靜態(tài)路由實現_DOS/BAT
- 2023-03-22 Python利用模糊哈希實現對比文件相似度_python
- 2022-07-18 通過注冊表實現程序開機自啟動的方法
- 2022-04-16 python中defaultdict字典功能特性介紹_python
- 2023-02-10 C/C++開發(fā)中extern的一些使用注意事項_C 語言
- 2022-09-06 Redis與本地緩存的結合實現_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支