简体   繁体   English

固定高度为100%的Div

[英]Div with fixed height at 100%

So I am trying to get a div to cover 100% height of the window which is simple but then I do not want it to resize when the browser is resized. 所以我试图让一个div覆盖窗口的100%的高度,这很简单,但是我不希望在调整浏览器大小时调整它的大小。 I cannot used a fixed height such as - height: 900px as it will differ between browser and device being used. 我不能使用固定高度,例如-height:900px,因为浏览器和所用设备之间会有所不同。 I have tried to use the following jquery: 我尝试使用以下jquery:

$(document).ready(function() {
    var screenHeight = $(window).height();
    $('#home').css('height', screenHeight + 'px');
});

This works to a certain degree but the problem is, if the site is visited while the browser is not in full screen it will set it to that height and then won't get bigger, I need it to set to the height of the browser IF the browser was maximized, is that possible? 这在一定程度上可行,但是问题是,如果在未全屏浏览器的情况下访问该网站,则会将其设置为该高度,然后不会变大,我需要将其设置为浏览器的高度如果浏览器已最大化,那可能吗?

You can see what I have so far at: http://zimxtrial.ukbigbuy.com/ - try refreshing the page with different browser heights and then resizing to see what I mean. 您可以在以下位置查看我到目前为止的内容:http: //zimxtrial.ukbigbuy.com/-尝试使用不同的浏览器高度刷新页面,然后调整大小以了解我的意思。

You could use screen.height or screen.availHeight (gets the screen resolution rather than the browser viewport): 您可以使用screen.heightscreen.availHeight (获取屏幕分辨率,而不是浏览器视口):

Example

use $(window).resize it will update the selector whenever browser height changes 使用$(window).resize它会在浏览器高度变化时更新选择器

Try this 尝试这个

<script type='text/javascript'>

$(function(){
    var iniHeight = $(window).height();
    $('#home').css('height', iniHeight + 'px');
    $(window).resize(function(){
    var newHeight = $(window).height();
    $('#home').css('height', newHeight + 'px');
    });
});
</script>

Try to use resize() like, 尝试像这样使用resize()

$(document).ready(function() {
    var screenHeight = $(window).height();
    $('#home').css('height', screenHeight + 'px');
    $(window).resize(function(){ // change home height on window resize event
       $('#home').css('height', $(this).height()+ 'px');
    });
});

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

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