动态设置DIV的高度

时间:2008-08-28 18:52:53

标签: javascript html css

在Web应用程序中,我有一个页面,其中包含一个具有自动宽度的DIV,具体取决于浏览器窗口的宽度。

我需要对象的自动高度。 DIV从顶部屏幕开始大约300px,它的高度应该使它伸展到浏览器屏幕的底部。我有一个容器DIV的最大高度,所以必须有div的最小高度。我相信我可以在CSS中限制它,并使用Javascript来处理DIV的大小调整。

我的javascript并不像它应该的那么好。有没有一个我能写的简单脚本可以为我做这个?

编辑: DIV包含一个控件,它可以执行自己的溢出处理(实现自己的滚动条)。

8 个答案:

答案 0 :(得分:31)

尝试这个简单的特定功能:

function resizeElementHeight(element) {
  var height = 0;
  var body = window.document.body;
  if (window.innerHeight) {
      height = window.innerHeight;
  } else if (body.parentElement.clientHeight) {
      height = body.parentElement.clientHeight;
  } else if (body && body.clientHeight) {
      height = body.clientHeight;
  }
  element.style.height = ((height - element.offsetTop) + "px");
}

它不依赖于指定身体顶部的当前距离(如果您的300px更改)。


编辑:顺便说一下,每次用户更改浏览器的大小时,你都希望在该div上调用它,所以你需要为此事件处理程序连接。

答案 1 :(得分:9)

溢出时应该怎么办?如果您希望它只是到达窗口的底部,请使用绝对定位:

div {
  position: absolute;
  top: 300px;
  bottom: 0px;
  left: 30px;
  right: 30px;
}

这将使DIV 30px从每侧进入,距离屏幕顶部300px,并与底部齐平。添加overflow:auto;以处理内容大于div的情况。

<小时/> 编辑:@Woeever标记这个,解释会很好......答案有问题吗?

答案 2 :(得分:3)

document.getElementById('myDiv').style.height = 500;

这是动态调整对象高度所需的非常基本的JS代码。我只是做了这件事,我有一些自动高度属性,但当我通过XMLHttpRequest添加一些内容时,我需要调整我的父div,这个offsetheight属性在IE6 / 7和FF3中做了诀窍

答案 3 :(得分:0)

DIV装有一个控件,可以自行进行溢出处理。

答案 4 :(得分:0)

如果我明白你在问什么,这应该可以解决问题:

// the more standards compliant browsers (mozilla/netscape/opera/IE7) use 
// window.innerWidth and window.innerHeight

var windowHeight;

if (typeof window.innerWidth != 'undefined')
{
    windowHeight = window.innerHeight;
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first 
// line in the document)
else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth != 'undefined' 
        && document.documentElement.clientWidth != 0)
{
    windowHeight = document.documentElement.clientHeight;
}
// older versions of IE
else
{
    windowHeight = document.getElementsByTagName('body')[0].clientHeight;
}

document.getElementById("yourDiv").height = windowHeight - 300 + "px";

答案 5 :(得分:0)

稍作修正:

function rearrange()
{
var windowHeight;

if (typeof window.innerWidth != 'undefined')
{
    windowHeight = window.innerHeight;
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first
// line in the document)
else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth != 'undefined'
        && document.documentElement.clientWidth != 0)
{
    windowHeight = document.documentElement.clientHeight;
}
// older versions of IE
else
{
    windowHeight = document.getElementsByTagName('body')[0].clientHeight;
}

document.getElementById("foobar").style.height = (windowHeight - document.getElementById("foobar").offsetTop  - 6)+ "px";
}

答案 6 :(得分:0)

最简单的我可以上来......

function resizeResizeableHeight() {
    $('.resizableHeight').each( function() {
        $(this).outerHeight( $(this).parent().height() - ( $(this).offset().top - ( $(this).parent().offset().top + parseInt( $(this).parent().css('padding-top') ) ) ) )
    });
}

现在你所要做的就是将resizableHeight类添加到你想要自动调整大小的所有内容(到它的父级)。

答案 7 :(得分:0)

受到@ jason-bunting的启发,高度或宽度相同:

function resizeElementDimension(element, doHeight) {
  dim = (doHeight ? 'Height' : 'Width')
  ref = (doHeight ? 'Top' : 'Left')

  var x = 0;
  var body = window.document.body;
  if(window['inner' + dim])
    x = window['inner' + dim]
  else if (body.parentElement['client' + dim])
    x = body.parentElement['client' + dim]
  else if (body && body['client' + dim])
    x = body['client' + dim]

  element.style[dim.toLowerCase()] = ((x - element['offset' + ref]) + "px");
}