Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript在没有溢出的情况下获取视口高度_Javascript_Html_Css_Jquery Mobile_Responsive Design - Fatal编程技术网

Javascript在没有溢出的情况下获取视口高度

Javascript在没有溢出的情况下获取视口高度,javascript,html,css,jquery-mobile,responsive-design,Javascript,Html,Css,Jquery Mobile,Responsive Design,我想得到一个屏幕的视口高度,没有溢出 换句话说,我希望看到用户屏幕的高度,而不是整个网站的页面高度,包括用户可以向下滚动的溢出量 所以,如果他有一部iphone,iphone的高度是600px,那么我想要的是600,而不是包括卷轴在内的整个身体,卷轴可以更高 这工作很好,但它是黑客,有其他解决方案吗?我没能想出别的办法 // First you forcibly request the scroll bars to be shown regardless if they will be need

我想得到一个屏幕的视口高度,没有溢出

换句话说,我希望看到用户屏幕的高度,而不是整个网站的页面高度,包括用户可以向下滚动的溢出量

所以,如果他有一部iphone,iphone的高度是600px,那么我想要的是600,而不是包括卷轴在内的整个身体,卷轴可以更高

这工作很好,但它是黑客,有其他解决方案吗?我没能想出别的办法

// First you forcibly request the scroll bars to be shown regardless if they will be needed or not.
$('body').css('overflow', 'scroll');

// Take your measures.
// (These measures WILL take into account scroll bars dimensions)
var heightWithScrollBars = $(window).height();
var widthWithScrollBars = $(window).width();

// Set the overflow css property back to it's original value (default is auto)
$('body').css('overflow', 'auto');

不完全确定我是否理解,但如果您需要用户的视口/屏幕大小 您可以像这样通过香草javascript访问它们

var viewportWidth = window.innerWidth + "px";
var viewportHeight = window.innerHeight + "px";

它只会给你一个数字,所以你必须在它前面加上px

不完全确定我是否理解,但如果您需要用户的视口/屏幕大小 您可以像这样通过香草javascript访问它们

var viewportWidth = window.innerWidth + "px";
var viewportHeight = window.innerHeight + "px";

它只会给你一个数字,所以你必须在它前面加上px

视口高度和宽度可通过以下方式访问:

width = window.innerWidth
height = window.innerHeight

可以通过以下方式访问视口高度和宽度:

width = window.innerWidth
height = window.innerHeight