首先看问题
屏幕是375*667
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=0" /> -->
<meta name="viewport" content="width=device-width, initial-scale=2.0" />
<title>Document</title>
<style>
* {
margin: 0;
}
#test {
height: 100px;
background-color: aquamarine;
}
</style>
</head>
<body>
<div id="test"></div>
<script>
let div = document.getElementById("test");
console.log(window.innerWidth); //375
console.log(document.documentElement.clientWidth); //375
div.style.width = 970 + "px";
console.log(window.innerWidth); //971
console.log(document.documentElement.clientWidth); //375
</script>
</body>
</html>
此代码在桌面端正常,在移动端有些异常,移动端的window.innerWidth不是视口大小,会变化
解决办法就是<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=0" />
,添加user-scalable=0