Device and Viewport Size In JavaScript

Viewport: How to get viewport size.

jQuery(window).width() uses document.documentElement.clientWidth 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.viewportW()
$(window).width() undefined
document.documentElement.clientWidth undefined
window.innerWidth undefined
window.outerWidth undefined
height methods live output
@media breakpoint
verge.viewportH()
$(window).height() undefined
document.documentElement.clientHeight undefined
window.innerHeight undefined
window.outerHeight 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.screen.width undefined
window.screen.availWidth undefined
height methods live output
@media breakpoint
window.screen.height undefined
window.screen.availHeight undefined

Document: How to get document size.

Document size methods are often needed in scrolling apps. Note the difference betweenjQuery(document).width() 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.documentElement properties seems to suffice.

width methods live output
$(document).width() undefined
document.body.clientWidth undefined
document.body.offsetWidth undefined
document.body.scrollWidth undefined
document.documentElement.clientWidth undefined
document.documentElement.offsetWidth undefined
document.documentElement.scrollWidth undefined
height methods live output
$(document).height() undefined
document.body.clientHeight undefined
document.body.offsetHeight undefined
document.body.scrollHeight undefined
document.documentElement.clientHeight undefined
document.documentElement.offsetHeight undefined
document.documentElement.scrollHeight undefined

转自:http://ryanve.com/lab/dimensions/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值