简体   繁体   English

调整窗口大小时重置div高度

[英]Resetting div height when window is resized

I'm trying to set the height on a responsive layout using javascript. 我正在尝试使用javascript在响应式布局上设置高度。 I notice that when using the following function the height does get applied when making the window narrow but doesn't get reset when moving back to the original wider window position. 我注意到使用以下功能时,使窗口变窄时会应用高度,但是当移回原始的较宽窗口位置时不会重置高度。 The height is retained no matter what the window size is. 无论窗口大小如何,高度都将保留。 How do I reset the div to the original height? 如何将div重置为原始高度?

$(function () {

    $(window).on("load resize", function () {
        console.log($(this).width());
        if ($(this).width() < 1025) {
            $(".case-study-child").height($(".case-study-parent").height());
        } 
    }).trigger('resize');

});

The best way to do this is with proper arrangement of the markup and use of responsive styles. 最好的方法是正确安排标记并使用响应样式。 However there may be a reason that responsive style is insufficient and it needs to be solved with js: 但是,可能有一个原因,即响应式样式不足,需要使用js解决:

You'll need to define the 'full screen' height of the .case-study-child div so it can be set when the screen width goes above 1024. This is necessary in case the page is originally loaded at < 1024 您需要定义.case-study-child div的“全屏”高度,以便可以在屏幕宽度超过1024时进行设置。如果页面最初加载时<1024

$(function () {

    // need to know what the div height should be when the page width is >= 1025px
    window.fullScreenHeight = "250px";

    $(window).on("load resize", function () {

        console.log($(this).width());

        // get the height to set the div to based on the screen width
        var newDivHeight = $(this).width() < 1025 ? $(".case-study-parent").height() : window.fullScreenHeight;

        $(".case-study-child").height(newDivHeight);

    }).trigger("resize");

});

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

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