为什么我的rem配置失败,不能随着窗口的变化让内容自适应?
这是配置rem.js的文件
function remSize() {
// 获取设备的宽度
var deviceWidth = document.documentElement.clientWidth || window.innerWidth;
if (deviceWidth >= 750) {
deviceWidth = 750;
}
if (deviceWidth <= 320) {
deviceWidth = 320;
}
// 这是html的
document.documentElement.style.fontSize = deviceWidth / 7.5 + "px";
// 设置字体大小 350/7.5 * 0.3
document.querySelector("body").style.fontSize = 0.3 + "rem";
}
remSize();
// DOM事件,当浏览器窗口大小变化时,自动会调用这个函数
window.onresize = function () {
remSize();
};
到index.html去调用
代码在倒数第三行的位置
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<!-- 引入图标 -->
<script src="//at.alicdn.com/t/c/font_3652123_ca4x3n3wg19.js"></script>
<title><%= htmlWebpackPlugin.options.title %></title>
<style type="text/css">
.icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
</style>
</head>
<body>
<noscript>
<strong
>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work
properly without JavaScript enabled. Please enable it to
continue.</strong
>
</noscript>
<div id="app"></div>
<!-- 把写的适配代码引入过来 -->
<script src="<%= BASE_URL %>js/rem.js"></script>
</body>
</html>
当我改变窗口大小,但内容大小没自适应

