简体   繁体   English

如何使用jQuery检测窗口大小?

[英]How can I detect window size with jQuery?

How can I detect window/browser size with jQuery like Gmail? 如何使用jQuery之类的Gmail检测窗口/浏览器大小? In Gmail, we don't need to refresh or reload current page as soon as we have changed window resolution in window setting? 在Gmail中,我们在窗口设置中更改窗口分辨率后,无需刷新或重新加载当前页面? In my project, I need to refresh the browser as soon as window setting has been changed. 在我的项目中,我需要在窗口设置更改后立即刷新浏览器。

Any idea will be appreciated. 任何想法将不胜感激。

You cannot really find the display resolution from a web page. 您无法从网页中找到显示分辨率。 There is a CSS Media Queries statement for it, but it is poorly implemented in most devices and browsers, if at all. 这里它的一个CSS媒体查询语句,但它在大多数设备和浏览器中执行不力的,如果在所有。 However, you do not need to know the resolution of the display, because changing it causes the (pixel) width of the window to change, which can be detected using the methods others have described: 但是,您不需要知道显示器的分辨率,因为更改它会导致窗口的(像素)宽度发生变化,这可以使用其他人描述的方法进行检测:

$(window).resize(function() {
  // This will execute whenever the window is resized
  $(window).height(); // New height
  $(window).width(); // New width
});

You can also use CSS Media Queries in browsers that support them to adapt your page's style to various display widths, but you should really be using em units and percentages and min-width and max-width in your CSS if you want a proper flexible layout. 您也可以在支持它们的浏览器中使用CSS Media Queries来调整页面的样式以适应各种显示宽度,但如果您想要一个合适的灵活布局,您应该在CSS中使用em单位和百分比以及min-widthmax-width Gmail probably uses a combination of all these. Gmail可能会综合使用所有这些内容。

You make one div somewhere on the page and put this code: 你在页面的某个地方创建一个div并输入以下代码:

<div id="winSize"></div>
<script>
    var WindowsSize=function(){
        var h=$(window).height(),
            w=$(window).width();
        $("#winSize").html("<p>Width: "+w+"<br>Height: "+h+"</p>");
    };
   $(document).ready(WindowsSize); 
   $(window).resize(WindowsSize); 
</script>

Here is a snippet: 这是一个片段:

 var WindowsSize=function(){ var h=$(window).height(), w=$(window).width(); $("#winSize").html("<p>Width: "+w+"<br>Height:"+h+"</p>"); }; $(document).ready(WindowsSize); $(window).resize(WindowsSize); 
 #winSize{ position:fixed; bottom:1%; right:1%; border:rgba(0,0,0,0.8) 3px solid; background:rgba(0,0,0,0.6); padding:5px 10px; color:#fff; text-shadow:#000 1px 1px 1px,#000 -1px 1px 1px; z-index:9999 } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="winSize"></div> 

Of course, adapt it to fit your needs! 当然,根据您的需求进行调整! ;) ;)

You can get the values for the width and height of the browser using the following: 您可以使用以下方法获取浏览器宽度和高度的值:

$(window).height();
$(window).width();

To get notified when the browser is resized, use this bind callback: 要在调整浏览器大小时收到通知,请使用此绑定回调:

$(window).resize(function() {
    // Do something
});

Do you want to detect when the window has been resized? 您想要检测窗口调整大小的时间吗?

You can use JQuery's resize to attach a handler. 您可以使用JQuery的resize来附加处理程序。

//get dimensions 
var height = $(window).height();
var width = $(window).width();

//refresh on resize
$(window).resize(function() {
  location.reload(true)
});

not sure if you wanted to tinker with the dimensions of elements or actually refresh the page. 不确定您是否想要修改元素的尺寸或实际刷新页面。 so here a bunch of different things pick what you want. 所以这里有很多不同的东西选择你想要的东西。 you can even put the height and width in the resize event if you really wanted. 如果你真的想要,你甚至可以把高度和宽度放在resize事件中。

You could also use plain Javascript window.innerWidth to compare width. 您还可以使用普通的Javascript window.innerWidth来比较宽度。

But use jQuery's .resize() fired automatically for you: 但是使用jQuery的.resize()自动为你启动:

$( window ).resize(function() {
  // your code...
}); 

http://api.jquery.com/resize/ http://api.jquery.com/resize/

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

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