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

发布于 2024-12-10 19:10:45 字数 125 浏览 0 评论 0原文

如何使用 jQuery(如 Gmail)检测窗口/浏览器大小?在 Gmail 中,当我们在窗口设置中更改窗口分辨率时,不需要立即刷新或重新加载当前页面?在我的项目中,窗口设置更改后我需要刷新浏览器。

任何想法将不胜感激。

How can I detect window/browser size with jQuery like Gmail? In Gmail, we don't need to refresh or reload current page as soon as we have changed window resolution in window setting? In my project, I need to refresh the browser as soon as window setting has been changed.

Any idea will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

偏爱自由 2024-12-17 19:10:45

您无法真正从网页上找到显示分辨率。有一个 CSS Media Queries 语句来实现它,但它在大多数设备和浏览器中实现得很差(如果有的话)。但是,您不需要知道显示器的分辨率,因为更改它会导致窗口的(像素)宽度发生变化,可以使用其他人描述的方法进行检测:

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

您还可以在支持 CSS 媒体查询的浏览器中使用 CSS 媒体查询,以使页面的样式适应各种显示宽度,但您实际上应该使用 em 单位和百分比以及 min-widthmax-width适当灵活的布局。 Gmail 可能使用所有这些的组合。

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. 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. Gmail probably uses a combination of all these.

无人问我粥可暖 2024-12-17 19:10:45

您在页面上的某处创建一个 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>

这是一个片段:

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>

当然,请根据您的需要进行调整! ;)

You make one div somewhere on the page and put this code:

<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! ;)

旧伤还要旧人安 2024-12-17 19:10:45

您可以使用以下方法获取浏览器的宽度和高度值:

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

要在调整浏览器大小时收到通知,请使用此绑定回调:

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

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
});
我的鱼塘能养鲲 2024-12-17 19:10:45

您想检测窗口何时调整大小吗?

您可以使用 JQuery 的 resize 来附加处理程序。

Do you want to detect when the window has been resized?

You can use JQuery's resize to attach a handler.

-柠檬树下少年和吉他 2024-12-17 19:10:45
//get dimensions 
var height = $(window).height();
var width = $(window).width();

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

不确定您是否想修改元素的尺寸或实际刷新页面。所以这里有很多不同的东西可以选择你想要的。如果您确实需要,您甚至可以将高度和宽度放入调整大小事件中。

//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.

云仙小弟 2024-12-17 19:10:45

您还可以使用纯 Javascript window.innerWidth 来比较宽度。

但是使用 jQuery 的 .resize() 自动为您触发:

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

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

You could also use plain Javascript window.innerWidth to compare width.

But use jQuery's .resize() fired automatically for you:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文