简体   繁体   English

根据窗口大小更改高度

[英]Change height based on window size

I want to change the height of a div to be percentage based as opposed to pixel based, I have the below code and I'm curious how i would now change it from pixels to percentage. 我想将div的高度更改为基于百分比而不是基于像素,我有以下代码,我很好奇现在如何将其从像素更改为百分比。

The end result is I want a div to re-size its height based on a percentage of the current height of the window. 最终结果是我希望div根据窗口当前高度的百分比重新调整其高度。

Edit: The height and width condition as per the code below isn't actually needed, but it's there for testing purposes as i was trying to find a solution. 编辑:实际不需要下面的代码的高度和宽度条件,但它是出于测试目的,因为我试图找到一种解决方案。

Please forgive my naivete, I'm relatively new to JavaScript. 请原谅我的天真,我是JavaScript的新手。

$(function(){
    $(window).resize(function(){
        var h = $(window).height();
        var w = $(window).width();
        $(".gallery-container").css('height',(h < 768 || w < 1024) ? 800 : 400);
    });
});

By the power of math! 通过数学的力量!

var h = $(window).height();
var scale = 0.75; // This is 75%
$(".gallery-container").css('height', h * scale);

According to your JavaScript code (I'm guessing it's not exactly correct) this is the corresponding solution in CSS: 根据您的JavaScript代码(我猜这不是完全正确),这是CSS中的相应解决方案:

.gallery-container { height: 400px; }

@media screen and (max-width: 1024px) or (max-height: 768px)  {
 .gallery-container { height: 800px; }
}

I found another solution that seems to be working perfectly at the moment. 我发现了另一个目前似乎运行良好的解决方案。

$(window).resize(function(){
   var newheight = $(window).innerHeight();      
   $(".gallery-container, #static ul li img").height(newheight / 2);
});

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

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