简体   繁体   English

动态设置 DIV 的高度

[英]Setting the height of a DIV dynamically

In a web application, I have a page that contains a DIV that has an auto-width depending on the width of the browser window.在 Web 应用程序中,我有一个包含 DIV 的页面,该 DIV 具有自动宽度,具体取决于浏览器窗口的宽度。

I need an auto-height for the object.我需要对象的自动高度。 The DIV starts about 300px from the top screen, and its height should make it stretch to the bottom of the browser screen. DIV 从顶部屏幕开始约 300 像素,其高度应使其延伸到浏览器屏幕的底部。 I have a max height for the container DIV, so there would have to be minimum-height for the div.我有容器 DIV 的最大高度,所以必须有最小高度的 div。 I believe I can just restrict that in CSS, and use Javascript to handle the resizing of the DIV.我相信我可以在 CSS 中限制它,并使用 Javascript 来处理 DIV 的大小调整。

My javascript isn't nearly as good as it should be.我的 javascript 并不像它应该的那么好。 Is there an easy script I could write that would do this for me?有没有我可以编写的简单脚本可以为我做到这一点?

Edit: The DIV houses a control that does it's own overflow handling (implements its own scroll bar).编辑:DIV 包含一个控件,它执行自己的溢出处理(实现自己的滚动条)。

Try this simple, specific function:试试这个简单、具体的函数:

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");
}

It does not depend on the current distance from the top of the body being specified (in case your 300px changes).它不取决于指定的身体顶部的当前距离(以防您的 300px 发生变化)。


EDIT: By the way, you would want to call this on that div every time the user changed the browser's size, so you would need to wire up the event handler for that, of course.编辑:顺便说一句,每次用户更改浏览器的大小时,您都希望在该 div 上调用它,因此您当然需要为此连接事件处理程序。

What should happen in the case of overflow?在溢出的情况下应该发生什么? If you want it to just get to the bottom of the window, use absolute positioning:如果您希望它只是到达窗口底部,请使用绝对定位:

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

This will put the DIV 30px in from each side, 300px from the top of the screen, and flush with the bottom.这会将 DIV 从每边 30px 放入,距屏幕顶部 300px,并与底部齐平。 Add an overflow:auto;添加一个overflow:auto; to handle cases where the content is larger than the div.处理内容大于div的情况。


Edit: @Whoever marked this down, an explanation would be nice... Is something wrong with the answer? 编辑:@Whoever 将其标记下来,解释会很好......答案有问题吗?

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

This is the very basic JS code required to adjust the height of your object dynamically.这是动态调整对象高度所需的非常基本的 JS 代码。 I just did this very thing where I had some auto height property, but when I add some content via XMLHttpRequest I needed to resize my parent div and this offsetheight property did the trick in IE6/7 and FF3我只是在我有一些自动高度属性的地方做了这件事,但是当我通过XMLHttpRequest添加一些内容时,我需要调整我的父 div 的大小,而这个 offsetheight 属性在 IE6/7 和 FF3 中起到了作用

With minor corrections:稍加修正:

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";
}

Simplest I could come up...我能想到的最简单的...

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

Now all you have to do is add the resizableHeight class to everything you want to autosize (to it's parent).现在您要做的就是将 resizableHeight 类添加到您想要自动调整大小的所有内容(到它的父项)。

If I understand what you're asking, this should do the trick:如果我明白你在问什么,这应该可以解决问题:

// 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";

inspired by @jason-bunting, same thing for either height or width:灵感来自@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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM