简体   繁体   English

我需要在加载页面和调整页面大小时根据窗口的大小设置div的高度

[英]I need to set the height of a div according to the size of the window when the page is loaded and when the page is resized

I need a function that sets the height of a div according to the size of the window when the page is loaded and when the page is resized. 我需要一个函数,该函数可以在加载页面和调整页面大小时根据窗口的大小设置div的高度。

Instead I have a function that only adds to the length of the div, only when the window is resized. 相反,我有一个仅在调整窗口大小时才增加div长度的函数。

I am not good at javascript myself, and this is the very last thing I need before my webpage is completed, so any help would be greatly appreciated! 我本人并不擅长javascript,这是我在完成网页之前需要做的最后一件事,因此,我们将不胜感激!

This is the page, and the div in question is the light gray infobox (containing text and a button) on the left: http://goodshitdesign.no/ 这是页面,相关的div是左侧的浅灰色信息框(包含文本和按钮): http : //goodshitdesign.no/

This is the code for the function I currently have: 这是我目前拥有的功能的代码:

<script>
$(document).ready(function() {
    $(window).on("resize", function() {

        var bodyheight = $(document).height();

        $(".infobox").height(bodyheight);
    });
});
</script>

No need of using Javascript to set the dimensions relative to the viewport. 无需使用Javascript设置相对于视口的尺寸。

Use viewport percentage height 使用视口百分比高度

1/100th of the height of the viewport. 视口高度的1/100。

.infobox {
    height: 100vh;
}

If you still want to use JS( I don't know why when using CSS will be better approach ), 如果您仍然想使用JS( 我不知道为什么在使用CSS时会更好 ),

  1. resize event binding is not required to be wrapped in ready resize事件绑定不需要ready
  2. To get window height use $(window).height() 要获取窗口高度,请使用$(window).height()
  3. Use trigger to force execute event handler on page load 使用trigger强制在页面加载时执行事件处理程序

Code: 码:

$(window).on("resize", function () {
    $(".infobox").height($(window).height());
    //                   ^^^^^^^^^
}).trigger('resize');
// ^^^^^^^^^^^^^^^^^

Have you considered using vh and vw CSS units instead of JavaScript? 您是否考虑过使用vhvw CSS单元代替JavaScript?

.infobox { width: 100vh; height: 100vh; }
<script>
$(document).ready(function() {
    $(".infobox").height($(window).height());
    $(window).on("resize", function() {
        $(".infobox").height($(window).height());
    });
});
</script>

'resize' do not run when page is loaded. 加载页面时,“调整大小”不运行。 Therefore on page ready you just create a event handler for resizes that will occur in the future. 因此,在准备好页面时,您只需创建一个事件处理程序以调整将来的大小。

your script works as it is. 您的脚本按原样工作。 in order to resize your div onload, simply call the .resize method without any parameters. 为了调整div onload的大小,只需调用不带任何参数的.resize方法即可。

<script>
$(document).ready(function() {
    $(window).on("resize", function() {
        var bodyheight = $(document).height();
        $(".infobox").height(bodyheight);
    });
    $(window).resize(); // THIS EMULATES THE RESIZE EVENT ONDOCUMENTREADY
});
</script>

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

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