简体   繁体   English

动态调整div在网页上的高度

[英]Dynamically resize height of div's on webpage

I'm basically using a 3-column layout for a website. 我基本上是为网站使用3列布局。 What I want to achieve is that the height of the middle (content) column is adjusted to the right column (sidebar), so the border to the right of the middle column reaches until the bottom of the page. 我要实现的是将中间(内容)列的高度调整为右侧列(侧边栏),以便中间列右侧的边框达到页面底部。 Furthermore, I also want the left column to adjust it's height to the other columns, and fill it with a background image unit the bottom of the page. 此外,我还希望左列将其高度调整为其他列,并在页面底部用背景图像单元填充它。

Website: http://www.massageforum.be 网址: http//www.massageforum.be

This is what I've been doing so far (panel_left = left column with background image, panel_right is a surrounding div for the div content (middle column) and sidebar (right column)): 这是我到目前为止所做的(panel_left =带有背景图像的左列,panel_right是div内容(中间列)和侧边栏(右列)的周围div):

function setDivHeight() {
    //set height of content according to sidebar if content is smaller than sidebar
    var DivHeightSidebar = document.getElementById('sidebar').offsetHeight;
    var DivHeightContent = document.getElementById('content').offsetHeight;

    if(DivHeightContent < DivHeightSidebar)
    {
        document.getElementById('content').style.height = DivHeightSidebar + 'px';
    }           

    //set height of panel_left according to panel_right so background image reaches until bottom of page
    var DivHeight = document.getElementById('panel_right').offsetHeight;
    document.getElementById('panel_left').style.height = DivHeight + 'px';
}

This function is called on every page just before the </body> tag. </body>标记之前的每个页面上都会调用此函数。

The problem is that is works sometimes, but sometimes not. 问题是有时有效,但有时无效。 In some cases the height of the content column is adjusted to the height of the sidebar column, although it should be longer and content doesn't fit anymore. 在某些情况下,内容栏的高度会调整为侧栏栏的高度,尽管它应该更长一些并且内容不再适合。 Screenshot: http://www.massageforum.be/Massageforum1.png 截图: http//www.massageforum.be/Massageforum1.png

Also, sometimes the height of the left column is not properly adjusted to the height of the panel_right. 另外,有时左列的高度未正确调整为panel_right的高度。

I see this behaviour in Chrome, Firefox and Safari; 我在Chrome,Firefox和Safari中看到了这种行为; didn't test the other browsers. 没有测试其他浏览器。

Any advice on how to make this function work more robust? 关于如何使此功能更强大的任何建议?

It's not a good idea to do that with JavaScript. 用JavaScript做到这一点不是一个好主意。 Just restructure the page and do it all with HTML/CSS. 只需重组页面并使用HTML / CSS完成所有操作即可。

Have you checked that the document finished loading all elements before calling setDivHeight? 您是否在调用setDivHeight之前检查文档是否已完成所有元素的加载? If the function is called before/while elements are still being loaded, the height you read would be smaller. 如果在元素仍在加载之前/期间调用此函数,则读取的高度会更小。 I would also consider setting height of contained columns to be equal to the container DIV height (id='page2'), which stretches naturally to the longest-contained element. 我还将考虑将包含列的高度设置为等于容器DIV高度(id ='page2'),该高度自然会扩展到包含时间最长的元素。

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

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