Device and Viewport Size In JavaScript
2016-04-06 16:31
302 查看
Viewport:
How to get viewport size.
jQuery(window).width()uses
document.documentElement.clientWidthand
is regarded as cross-browser compatible. Directly using
.clientWidthis 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
@mediabreakpoints.
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.widthfor device width and
window.screen.heightfor
device height.
.availWidthand
.availHeightgive 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 the
max-widthof the body). jQuery uses the
Math.maxof
5 numbers to calculate this. For decent cross-browser support, the
Math.maxof the 3
document.documentElementproperties 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 |
相关文章推荐
- JS 数组去重
- EL表达式获取地址栏地址以及jsp如何获取服务器信息
- EL表达式获取地址栏地址以及jsp如何获取服务器信息
- JavaScript中的slice函数
- JS中0跟""比较
- js的for..in语句的用法详解
- js图片滚动
- JS中六种数据类型(一)――undefined
- js事件冒泡和捕获
- js时间 字符串相互转化
- jsp与servlet
- VS 2008 jsoncpp的配置及使用实例
- js将类数组转成数组
- Javascript写入txt和读取txt文件示例
- JSON详解
- <%@include%>与<jsp:include>的区别
- 一个jsonp的简单实例
- Java与JavaScript 完美实现字符串拆分(利用数组存储)与合并的互逆操作
- JS中判断null、undefined与NaN的方法
- js中时间戳转化成时间格式