<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript">
    </script>
</head>
<body>
</body>
</html>

以上的代码运行结果如下

window.innerWidth和document.body.clientWidth的区别

因为body默认情况下具有margin,这个默认值为8px。window.innerWidth (1280px) - 2*8px = document.body.clientWidth(1264px)。

如果代码改成

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript">
    </script>
    <style type="text/css">
        *{
            margin: 0;
        }
    </style>
</head>
<body>
</body>
</html>

运行结果如下

window.innerWidth和document.body.clientWidth的区别

document.body.clientWidth和window.innerWidth的结果一样,都是1280px

相关文章:

  • 2021-05-10
  • 2021-11-02
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-08
  • 2021-06-16
猜你喜欢
  • 2021-08-20
  • 2021-12-09
  • 2022-01-18
  • 2021-12-26
  • 2022-02-14
  • 2022-12-23
相关资源
相似解决方案