代码之家  ›  专栏  ›  技术社区  ›  noamtm

html/css:将div bounding box设置为window height

  •  1
  • noamtm  · 技术社区  · 16 年前

    看来我错过了一些基本的东西。 我想要一个有窗户的高度。其效果应该是永远不需要向下滚动。

    我必须使用JavaScript吗?我被告知css有可能,但是我找不到相关的属性,或者我做错了什么。

    5 回复  |  直到 16 年前
        1
  •  4
  •   Strelok    16 年前

    body { 
      margin:0; 
      padding:0; 
      height:100%; /* this is the key! */ 
    }
    
    .yourDivStyle {
      ...
      height: 100%;
    } 
    
        2
  •  1
  •   hood    16 年前

    如果要关闭滚动,请使用以下CSS:

    html, body
    {
    overflow: hidden;
    }
    

    如果使用Javascript,请确保注册onresize事件,以便在调整窗口大小时更改数字。

        3
  •  1
  •   reisio    16 年前

    让所有的父母 height: 100%; position: absolute /* or fixed */; left: 0; top: 0; width: 100%; height: 100%; .

        4
  •  0
  •   Sarfraz    16 年前

    我必须使用JavaScript吗?我是 告诉我用css是可能的,但是 或者我做错了什么。

    Javascript就是这样,你不能用css来确定winodow的大小,并根据窗口大小重新计算。

        5
  •  -1
  •   Barrie Reader    16 年前

    好的,所以在Javascript中(不是jQuery或其他任何东西):

    <html>
    <head>
    <script type="text/javascript">
    <!--
    
     var sWidth;
     var sHeight;
    
    
     if (typeof window.innerWidth != 'undefined')
     {
          sWidth = window.innerWidth,
          sHeight = window.innerHeight
     } else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientWidth !=
         'undefined' && document.documentElement.clientWidth != 0)
     {
           sWidth = document.documentElement.clientWidth,
           sHeight = document.documentElement.clientHeight
     } else
     {
           sWidth = document.getElementsByTagName('body')[0].clientWidth,
           sHeight = document.getElementsByTagName('body')[0].clientHeight
     }
    //VARIABLES STORED IN sWidth AND sHeight
    document.write('<div style="height: '+sHeight+'; width: '+sWidth+';">CONTENT</div>');
    //-->
    </script>
    </head>
    <body>
    
    </body>
    </html>