手机端适配rem代码片段

时间:2023-12-26 09:12:07
<!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>
<script> function getRem (){
var currentWidth = document.documentElement.clientWidth;
var fontSize = 100;
var width = 320;
var currentFontsize = currentWidth / width * fontSize;
document.querySelector('html').style.fontSize = currentFontsize + 'px';
}
getRem();
window.onresize = function(){
getRem();
}
</script>
<style>
p {
font-size: 1rem;
}
</style>
<body>
<p>测试</p>
</body>
</html>