当页面大于屏幕时,我无法在 javascript 中获取页面的高度。
我认为这会让我得到正确的高度:
$(document).height();
但这只会获得屏幕的高度,与:
$('body').height();
如同:
document.offsetHeight;
由于某种原因,所有这些示例仅返回屏幕的高度。有人可以帮忙吗?
当页面大于屏幕时,我无法在 javascript 中获取页面的高度。
我认为这会让我得到正确的高度:
$(document).height();
但这只会获得屏幕的高度,与:
$('body').height();
如同:
document.offsetHeight;
由于某种原因,所有这些示例仅返回屏幕的高度。有人可以帮忙吗?
使用 jQuery(您确实指定了),$(document).height()
将准确返回您所要求的内容。
澄清该height()
方法的用法:
$('.someElement').height(); // returns the calculated pixel height of the element(s)
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document
我怀疑,如果$(document).height()
它不适合你,那就有问题了。你可能:
如果你喜欢使用 jQuery,那么获取 body 或 html 高度呢?像:
var winHeight = $('body').height();
我一直在寻找这个并降落在这里。如果没有 Jquery,你也可以使用普通的 JS 来获得它。以下作品:
console.log(document.body.clientHeight,document.body.scrollHeight,document.body.offsetHeight)
请注意以下链接以阐明使用哪个: 分别了解 offsetWidth、clientWidth、scrollWidth 和 -Height
我认为您需要添加window.pageYOffset
(页面滚动的数量,以像素为单位)。
不确定JQuery。但是此链接可能会帮助您了解各种属性以及它们在不同浏览器中的不同模式下的行为方式。
http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html