admin管理员组文章数量:1180531
I'm unable to get the height of a page in javascript when the page is larger than the screen.
I thought this would get me the right height:
$(document).height();
but that only gets the height of the screen, same as:
$('body').height();
same as:
document.offsetHeight;
For some reason all these examples only return the height of the screen. Can somebody help?
I'm unable to get the height of a page in javascript when the page is larger than the screen.
I thought this would get me the right height:
$(document).height();
but that only gets the height of the screen, same as:
$('body').height();
same as:
document.offsetHeight;
For some reason all these examples only return the height of the screen. Can somebody help?
Share Improve this question edited Jul 18, 2009 at 14:27 Jeff Atwood 63.9k48 gold badges151 silver badges153 bronze badges asked Jul 18, 2009 at 14:23 jzp74jzp74 6374 gold badges9 silver badges21 bronze badges 1 |5 Answers
Reset to default 22Using jQuery (which you did specify), $(document).height()
will return exactly what you're asking for.
To clarify the usage of the height()
method:
$('.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
I suspect, that if $(document).height()
is not working for you, something is wrong. You may be:
- Calling it too early. Like, before the DOM is ready
- Have some uncleared floats that are not causing some block level elements to expand to their real height. Thus messing up height calculations.
- Have something critical absolutely positioned. Absolutely positioned elements do not contribute towards height calculations of their parent elements.
If you're cool with using jQuery, what about getting the body or html height? like:
var winHeight = $('body').height();
I was looking for this and landed up here. Without Jquery, you can get this with plain JS also. Below works:
console.log(document.body.clientHeight,document.body.scrollHeight,document.body.offsetHeight)
Do note the below link to clarify which to use: Understanding offsetWidth, clientWidth, scrollWidth and -Height, respectively
I think you need to add window.pageYOffset
(the amount the page has been scrolled, in pixels).
http://www.quirksmode.org/dom/w3c_cssom.html
Not sure about JQuery. But this link, might help you to understand various properties and how they behave in different modes in different browsers.
http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
本文标签: jqueryheight of page in javascriptStack Overflow
版权声明:本文标题:jquery - height of page in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738193877a2068074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$(document).height()
has returned the height on the entire document since 1.2 – jason Commented Jul 18, 2009 at 14:30