简体   繁体   English

窗口垂直调整大小问题

[英]Window vertically re-sizing issue

Im trying to resize the window. 我正在尝试调整窗口大小。 it works horizentally but not vertically. 它可以水平运行,但不能垂直运行。 What am I doing wrong here: 我在这里做错了什么:

$(window).resize(resizeWebSite());

function resizeWebSite(){

    $("#panel").height($(window).height() - 10);
    $("#map").height($(window).height() - 10);

     console.log($("#map").height() / 2);   

     var loaderTop = ($("#map").height() - $("#loadingIndicator").height()) / 100 + $("#map").position().top;
     var loaderLeft = (($("#map").width() - $("#loadingIndicator").width()) / 100) + $("#map").position().left;


    $("#loadingIndicator").css({'position' : 'absolute' , 'left' : loaderLeft + 'px', 'top' : loaderTop + 'px'});    
}

resizeWebSite();

When you set up the event handler, you need to pass only the name of the function: 设置事件处理程序时,只需传递函数名称

$(window).resize(resizeWebSite);

Now a further problem with this will be that on a desktop/laptop machine (traditional computer, as opposed to a tablet), where the user can resize the browser window interactively, the browser will fire "resize" events very rapidly. 现在,与此有关的另一个问题是,在台式机/笔记本电脑(传统计算机,而不是平板电脑)上,用户可以交互地调整浏览器窗口的大小,浏览器将非常迅速地触发“调整大小”事件。 All that work inside the handler will make the response very sluggish and jumpy. 处理程序内部的所有工作都会使响应变得非常缓慢和跳跃。

To counteract that, you can use a timer. 为了解决这个问题,您可以使用计时器。 The idea is to respond to a "resize" event by first cancelling any pending timer, and then setting a new timer for some time in the future (a hundred milliseconds or so) to run the actual code that deals with the new window size. 这个想法是通过首先取消任何未决的计时器,然后在将来的某个时间(一百毫秒左右)中设置一个新的计时器,以运行处理新窗口大小的实际代码来响应“调整大小”事件。 That way, while the user is moving the mouse quickly to change the window size, the only work you're doing is clearing and resetting a timer, which is pretty fast. 这样,当用户快速移动鼠标以更改窗口大小时,您要做的唯一工作就是清除并重置计时器,这非常快。 Only when the user stops moving the mouse for a while do you actually do any real work. 只有当用户停止移动鼠标一会儿时,您才真正进行任何实际工作。

That would look something like this: 看起来像这样:

var resizeTimer = null;
$(window).resize(function() {
  clearTimeout(resizeTimer);
  resizeTimer = setTimeout(resizeWebSite, 100);
});

Maybe this is what you're after? 也许这就是你所追求的?

function resizeWebSite(){

    $("#panel").height($(window).height() - 10);
    $("#map").height($(window).height() - 10);

     console.log($("#map").height() / 2);   

     var loaderTop = ($("#map").height() - $("#loadingIndicator").height()) / 100 + $("#map").position().top;
     var loaderLeft = (($("#map").width() - $("#loadingIndicator").width()) / 100) + $("#map").position().left;


    $("#loadingIndicator").css({'position' : 'absolute' , 'left' : loaderLeft + 'px', 'top' : loaderTop + 'px'});    
}

$(document).ready(resizeWebSite);    
$(window).resize(resizeWebSite);

By the way, if you're using margin on the elements you're reading the height on, and you want to include them in the calculations, use outerWidth(true) instead of width() 顺便说一句,如果要在要读取高度的元素上使用margin ,并且希望将其包括在计算中,请使用outerWidth(true)而不是width()

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

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