使用jQuery获取没有滚动条的浏览器视口的高度和宽度? [英] Get the height and width of the browser viewport without scrollbars using jquery?

查看:102
本文介绍了使用jQuery获取没有滚动条的浏览器视口的高度和宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery在没有滚动条的情况下获取浏览器视口的高度和宽度?

How do I get the height and width of the browser viewport without scrollbars using jQuery?

这是我到目前为止尝试过的:

Here is what I have tried so far:

       var viewportWidth = $("body").innerWidth();
       var viewportHeight = $("body").innerHeight();

此解决方案未考虑浏览器滚动条.

This solution does not take into account the browser scrollbars.

推荐答案

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

更多信息

  • http://api.jquery.com/height/
  • http://api.jquery.com/width/

但是,使用jQuery对于获取这些值不是必不可少的.使用

Using jQuery is not essential for getting those values, however. Use

document.documentElement.clientHeight;
document.documentElement.clientWidth;

获取不包括滚动条的尺寸,或者

to get sizes excluding scrollbars, or

window.innerHeight;
window.innerWidth;

获取整个视口,包括滚动条.

to get the whole viewport, including scrollbars.

document.documentElement.clientHeight <= window.innerHeight;  // is always true

这篇关于使用jQuery获取没有滚动条的浏览器视口的高度和宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆