【发布时间】:2014-07-18 00:49:00
【问题描述】:
jQuery 文档http://api.jquery.com/height/ 说
// Returns height of browser viewport
$( window ).height();
// Returns height of HTML document
$( document ).height();
但是我从这两种方法中得到了相同的值。例如,(使用 html 包括溢出的 div 高度 = 约 3000px)
$( window ).height();
3588
$( document ).height();
3588
$("body").height();
3572
$("html").height();
3588
window.innerHeight;
667
它在 Chrome 和 Firefox 浏览器中给出了相同的结果。 (由于工具栏大小不同,数值略有不同。)
我对@987654325@ 的期望是“浏览器视口的高度”,即window.innerHeight = 667。但它给了3588,这比我预期的要大得多。
我理解错了吗?视口有什么不同吗?
无论如何,在Find the exact height and width of the viewport in a cross-browser way (no Prototype/jQuery) 中,解释了获取视口高度和宽度的方法。在本文档中,“视口”是我的想法,但不是 jQuery 文档中解释的那个。
================================================
最近编辑:
当我在我的计算机中打开离线 html 文件时,它只会给出错误的答案 3588。当我将 html 文件上传到我的博客并对其进行测试时,它给出了预期的正确答案 667。打开离线文件(file://)和打开在线html文件(http://)有什么区别吗?
我的测试 html 文件的简短版本。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div style="height:3500px; background:rgb(200,200,255)" id="result"></div>
<script>
$("#result").html("$(window).height():? "+$(window).height());
</script>
它仍然给予
$(window).height():? 3516
仅离线。
【问题讨论】:
-
创建一个需要滚动才能查看其内容的页面。 window 是你能看到的,document 是整个页面。
-
“窗口是我能看到的”。但是为什么
$(window).height()给出的数字(3588)比我的屏幕高度大? @Popnoodles -
这是在 iframe 内运行还是在什么地方运行?
-
我只是在控制台中运行它,还用一个 html 文件进行了测试。 @JamesMontagne
-
不。除非你使用的是 IE 并且它会进入兼容模式。
标签: jquery