Viewport: How to get viewport size.
jQuery(window) uses document and is regarded as cross-browser compatible. Directly using .clientWidth is faster and equally compatible. The tables below compare these live against the inner/outer methods to determine the most accurate method: verge wins because it normalizes browser nuances to accurately match @media breakpoints.
| width methods | live output |
|---|---|
| @media breakpoint | |
verge | |
$(window) | undefined |
document | undefined |
window | undefined |
window | undefined |
| height methods | live output |
|---|---|
| @media breakpoint | |
verge | |
$(window) | undefined |
document | undefined |
window | undefined |
window | undefined |
Device: How to get device size.
Use window.screen.width for device width and window.screen.height for device height. .availWidth and.availHeight give you the device size minus UI taskbars. (Try on an iPhone.) Device size is static and does not change when the page is resized or rotated.
| width methods | live output |
|---|---|
| @media breakpoint | |
window | undefined |
window | undefined |
| height methods | live output |
|---|---|
| @media breakpoint | |
window | undefined |
window | undefined |
Document: How to get document size.
Document size methods are often needed in scrolling apps. Note the difference betweenjQuery(document) and the native properties (especially when the window is wider than themax-width of the body). jQuery uses the Math.max of 5 numbers to calculate this. For decent cross-browser support, the Math.max of the 3 document properties seems to suffice.
| width methods | live output |
|---|---|
$(document) | undefined |
document | undefined |
document | undefined |
document | undefined |
document | undefined |
document | undefined |
document | undefined |
| height methods | live output |
|---|---|
$(document) | undefined |
document | undefined |
document | undefined |
document | undefined |
document | undefined |
document | undefined |
document | undefined |
本文介绍了如何使用不同方法获取浏览器视口尺寸、设备尺寸及文档尺寸,并对比了各种方法的准确性及其在不同媒体查询断点的表现。推荐使用 verge.viewportW 和 verge.viewportH 函数来获取准确的视口尺寸。
1024

被折叠的 条评论
为什么被折叠?



