【问题标题】:Height doesn't get updated when resizing the window调整窗口大小时高度不会更新
【发布时间】:2013-07-13 10:55:20
【问题描述】:

我目前在获取 html 元素的高度时遇到了一些问题,似乎在调整窗口大小时它没有得到更新。我尝试使用“高度:自动”重置 html,但这似乎没有帮助。

宽度正常,但高度没有更新。

有解决这个问题的办法吗?

提前致谢!

JQuery:

 function UpdateLeftBar() {
    // Reset
    $('html').height('auto');
    // Get the height of the window and minus the header and left top elements
    height = $('html').height() - 85 - 56 + 29;
    width = $('html').width() - 300;
    // Set the div to the new height
    $('#leftContentBottom').css({ 'height': height });
    $('#middleContent.span10').css({ 'width': width });
    console.log(height);
}

$(window).resize(function () {
    UpdateLeftBar();
});

真的很奇怪,我注意到它有 80% 的时间会更新,但如果你双击窗口,它不会调整大小。

修复:您应该获取窗口的高度,而不是获取 html 的高度。这解决了我的问题。

【问题讨论】:

  • 邮政编码。最好在 jsfiddle 中。
  • 我会做一个现场演示,等等!
  • 很遗憾,我的虚拟主机有问题,所以我无法设置现场演示。如果有任何问题,请随时提问! @m90,只是 html5 版本:
  • console.log(height); 的输出是什么?

标签: jquery html css


【解决方案1】:

感谢Huangism发重复贴,很容易解决我的问题。

解决方法是使用window而不是使用html元素来获取window的高度。似乎 html 并不总是更新。

        function UpdateLeftBar() {
    // Get the height of the window and minus the header and left top elements
    height = $(window).height() - 85 - 56;
    width = $('html').width() - 300;
    // Set the div to the new height
    $('#leftContentBottom').css({ 'height': height });
    $('#middleContent.span10').css({ 'width': width });
    console.log(height);
}

$(window).resize(function () {
    UpdateLeftBar();
});

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多