【问题标题】:Browser Window width smaller than expected when querying with JavaScript使用 JavaScript 查询时,浏览器窗口宽度小于预期
【发布时间】:2013-01-10 12:35:04
【问题描述】:

我需要使用窗口的高度和宽度以动态方式计算标题的宽度和高度。

问题是它们变小了 16 像素,我不知道为什么。

这是我期望得到的:

  1. w_height: 929, w_width: 1280
  2. body_height: 929 - 顶部 - 底部 - 边框(1px 顶部,1px 底部)= 727
  3. body_width: 1280 - 左 - 右 - 边框(1px 左,1px 右)= 878

这是我得到的:

  1. body_height: 711
  2. body_width: 867

您能告诉我如何解决这个问题以及为什么会发生这种情况吗? 谢谢。

<html>
    <head>
        <title>Dancing Web</title>
        <!--<link rel="stylesheet" href="css/index.css" />-->
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <style>
        body{
            position:absolute;
            top: 100px;
            left: 200px;
            right: 200px;
            bottom: 100px;  
        }
        figure{
            margin:0;
            padding:0;
        }

        .solid_border{
            border: 1px solid green;
        }
        </style>
    </head>
    <body class="solid_border">
        <header>

            <figure >

            </figure>
            <nav>

            </nav>
        </header>       
        <script type="text/javascript">
            $(document).ready(function(){          
                alert($(window).height() +', '+$(window).width());
                alert($('body').css('height') +', '+$('body').css('width'));        
            });
        </script>
    </body> 

</html>

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    你应该试试:

    alert($(window).outerHeight() +', '+$(window).outerWidth());
    

    【讨论】:

      【解决方案2】:

      这可能是因为你应该得到外部的宽度和高度。

      见:

      因为填充、边框,最后是边距。

      【讨论】:

        猜你喜欢
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-22
        • 2013-06-15
        • 2012-11-28
        • 1970-01-01
        • 2018-10-13
        相关资源
        最近更新 更多