简体   繁体   English

将div的高度更改为浏览器窗口高度,浏览器高度大于x

[英]change height of div to browser window height so long a the browser height is greater than x

Hi I'm hoping someone will be able to explain what i am doing wrong here: 嗨,我希望有人能够在这里解释我做错了什么:

 var winHeight = $(window).height(),
 minHeight = 900,
 Height = 900;

 $(document).ready(function () {

 if (winHeight < MinHeight) {
     Height = minHeight;
 } else {
     Height = winHeight;
 }
 $('div.page').css('height', winHeight + 'px');
});


 $(window).resize(function () {
 if (winHeight < MinHeight) {
     Height = minHeight;
 } else {
     Height = winHeight;
 }
 $('div.page').css('height', winHeight + 'px');

});

On my page I have multiple divs with the class "page". 在我的页面上,我有多个带有“页面”类的div。 I'm trying make the height of these the size of the browser window, unless the browser window is less than 900, then I want them to be 900px tall. 我正在尝试将这些大小设置为浏览器窗口的大小,除非浏览器窗口小于900,然后我希望它们是900px高。

I'm guessing its a syntax issue. 我猜它是一个语法问题。 Especially since I'm brand new to jquery and javascript ( I only started using it today). 特别是因为我是jquery和javascript的新手(我今天才开始使用它)。

You have to call $(window).height() on the resize event, so you can respond to the current window size. 你必须在resize事件上调用$(window).height() ,这样你就可以响应当前的窗口大小。 You can go with this: 你可以这样:

$(document).ready(function () {
    //Call the method one time
    updateWindowSize();
    //Subscribe the method to future resize events
    $(window).resize(updateWindowSize);

    //updateWindowSize inside a closure to hide 'minHeight'
     var updateWindowSize = (function(){
        var minHeight = 900;

        //Actual updateWindowSize function
        return function(){
            var winHeight = $(window).height();
            var newHeight = winHeight < minHeight ? minHeight : winHeight;
            $('div.page').height(newHeight);
        }
     })();
});

The problem is the value of winHeight is never changed when window resizes. 问题是当窗口调整大小时,winHeight的值永远不会改变。 And I wonder where the variable "Height" is used ? 我想知道变量“Height”在哪里使用?

The code should be: 代码应该是:

$(document).ready(function () {  
  $(window).resize(function () {
    var winHeight = $(window).height(),
    minHeight = 900,
    Height = 900;
    if (winHeight < minHeight) {
       Height = minHeight;
    } else {
       Height = winHeight;
    }

    $('div.page').height(Height);
  });
});

Make sense? 说得通?

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

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