目录
react或umi移动端适配
index.jsx或index.tsx
import React from 'react'
import './index.less'
export default function Index() {
function adapter() {
// 获取当前手机宽度->当前视口宽度->clientWidth
const deviceWidth = document.documentElement.clientWidth;
// 计算根字体字体大小
const rootFontSize = deviceWidth * 100 / 375;
// 设置根字体
document.documentElement.style.fontSize = String(rootFontSize)+'px'
}
adapter()
// 当窗口改变时执行
window.onresize = adapter
return (
<div content="width-device-width, initial-scale=1.0">
<div className='box'>
</div>
</div>
)
}
index.less
.box{
width: 3.45rem;
height: 3rem;
background-color: aqua;
margin: 0.15rem;
}
js移动端适配
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div class='box'></div>
</body>
<script>
function adapter() {
// 获取当前手机宽度->当前视口宽度->clientWidth
const deviceWidth = document.documentElement.clientWidth;
// 计算根字体字体大小
const rootFontSize = deviceWidth * 100 / 375;
// 设置根字体
document.documentElement.style.fontSize = String(rootFontSize)+'px'
}
adapter()
// 当窗口改变时执行
window.onresize = adapter
</script>
</html>