如何使用 jQuery 检测窗口大小?
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您无法真正从网页上找到显示分辨率。有一个 CSS Media Queries 语句来实现它,但它在大多数设备和浏览器中实现得很差(如果有的话)。但是,您不需要知道显示器的分辨率,因为更改它会导致窗口的(像素)宽度发生变化,可以使用其他人描述的方法进行检测:
您还可以在支持 CSS 媒体查询的浏览器中使用 CSS 媒体查询,以使页面的样式适应各种显示宽度,但您实际上应该使用
em
单位和百分比以及min-width
和max-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:
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 andmin-width
andmax-width
in your CSS if you want a proper flexible layout. Gmail probably uses a combination of all these.您在页面上的某处创建一个 div 并放置此代码:
这是一个片段:
当然,请根据您的需要进行调整! ;)
You make one div somewhere on the page and put this code:
Here is a snippet:
Of course, adapt it to fit your needs! ;)
您可以使用以下方法获取浏览器的宽度和高度值:
要在调整浏览器大小时收到通知,请使用此绑定回调:
You can get the values for the width and height of the browser using the following:
To get notified when the browser is resized, use this bind callback:
您想检测窗口何时调整大小吗?
您可以使用 JQuery 的 resize 来附加处理程序。
Do you want to detect when the window has been resized?
You can use JQuery's resize to attach a handler.
不确定您是否想修改元素的尺寸或实际刷新页面。所以这里有很多不同的东西可以选择你想要的。如果您确实需要,您甚至可以将高度和宽度放入调整大小事件中。
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.
您还可以使用纯 Javascript
window.innerWidth
来比较宽度。但是使用 jQuery 的
.resize()
自动为您触发: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:http://api.jquery.com/resize/