如何使用 jQuery 获取 div 完整内容的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6853293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the height of a div's full content with jQuery?
提问by mtlca401
I am trying to create my own scroll bars. I have tried most of the jquery scrollbar plugins and none of them seem to work for me, so I decided to create my own. I have an overflow area with scrollable content. I can get the scrollbar to work if I am able to figure out the height of the scrollable content area. I have tried .scrollHeight() and the browser doesn't even recognize it. The overflow area has a fixed position.
我正在尝试创建自己的滚动条。我已经尝试了大部分 jquery 滚动条插件,但似乎没有一个适合我,所以我决定创建自己的。我有一个带有可滚动内容的溢出区域。如果我能够计算出可滚动内容区域的高度,我就可以让滚动条工作。我试过 .scrollHeight() 并且浏览器甚至无法识别它。溢出区有一个固定的位置。
回答by mu is too short
scrollHeight
is a propertyof a DOM object, not a function:
scrollHeight
是一个属性一个的DOM对象,而不是一个函数:
Height of the scroll view of an element; it includes the element padding but not its margin.
元素滚动视图的高度;它包括元素填充但不包括它的边距。
Given this:
鉴于这种:
<div id="x" style="height: 100px; overflow: hidden;">
<div style="height: 200px;">
pancakes
</div>
</div>
This yields 200:
这产生 200:
$('#x')[0].scrollHeight
For example: http://jsfiddle.net/ambiguous/u69kQ/2/(run with the JavaScript console open).
例如:http: //jsfiddle.net/ambiguous/u69kQ/2/(在 JavaScript 控制台打开的情况下运行)。
回答by Shazboticus S Shazbot
If you can possibly help it, DO NOT USE .scrollHeight.
如果您可以提供帮助,请不要使用 .scrollHeight。
.scrollHeight does not yield the same kind of results in different browsers in certain circumstances (most prominently while scrolling).
.scrollHeight 在某些情况下(在滚动时最突出)在不同浏览器中不会产生相同类型的结果。
For example:
例如:
<div id='outer' style='width:100px; height:350px; overflow-y:hidden;'>
<div style='width:100px; height:150px;'></div>
<div style='width:100px; height:150px;'></div>
<div style='width:100px; height:150px;'></div>
<div style='width:100px; height:150px;'></div>
<div style='width:100px; height:150px;'></div>
<div style='width:100px; height:150px;'></div>
</div>
If you do
如果你这样做
console.log($('#outer').scrollHeight);
console.log($('#outer').scrollHeight);
you'll get 900px in Chrome, FireFox and Opera. That's great.
您将在 Chrome、FireFox 和 Opera 中获得 900px。那太棒了。
But, if you attach a wheelevent / wheel event to #outer, when you scroll it, Chrome will give you a constant value of 900px (correct) but FireFox and Opera will change their values as you scroll down (incorrect).
但是,如果您将wheelevent / wheel 事件附加到#outer,当您滚动它时,Chrome 会给您一个恒定值900px(正确),但FireFox 和Opera 会在您向下滚动时更改它们的值(不正确)。
A very simple way to do this is like so (a bit of a cheat, really). (This example works while dynamically adding content to #outer as well):
一个非常简单的方法就是这样(真的有点作弊)。(此示例在向#outer 动态添加内容的同时也有效):
$('#outer').css("height", "auto");
var outerContentsHeight = $('#outer').height();
$('#outer').css("height", "350px");
console.log(outerContentsHeight); //Should get 900px in these 3 browsers
The reason I pick these three browsers is because all three can disagree on the value of .scrollHeight in certain circumstances. I ran into this issue making my own scrollpanes. Hope this helps someone.
我选择这三个浏览器的原因是因为在某些情况下,这三个浏览器在 .scrollHeight 的值上可能存在分歧。我在制作自己的滚动窗格时遇到了这个问题。希望这可以帮助某人。
回答by Anmol Saraf
We can also use -
我们还可以使用——
$('#x').prop('scrollHeight') <!-- Height -->
$('#x').prop('scrollWidth') <!-- Width -->
回答by Sajid
Element.scrollHeight
is a property, not a function, as noted here. As noted here, the scrollHeight property is only supported after IE8. If you need it to work before that, temporarily set the CSS overflow
and height
to auto
, which will cause the div to take the maximum height it needs. Then get the height, and change the properties back to what they were before.
Element.scrollHeight
是一个属性,而不是一个函数,正如这里所指出的。如前所述这里的scrollHeight属性属性仅IE8后支持。如果您需要它在此之前工作,请暂时将 CSSoverflow
和设置height
为auto
,这将导致 div 达到所需的最大高度。然后获取高度,并将属性改回之前的状态。
回答by open and free
You can get it with .outerHeight()
.
你可以用.outerHeight()
.
Sometimes, it will return 0
. For the best results, you can call it in your div
's ready event.
有时,它会返回0
。为了获得最佳结果,您可以在您div
的 ready 事件中调用它。
To be safe, you should not set the height of the div
to x
. You can keep its height auto
to get content populated properly with the correct height.
为安全起见,您不应将高度设置div
为x
。您可以保持其高度auto
以使用正确的高度正确填充内容。
$('#x').ready( function(){
// alerts the height in pixels
alert($('#x').outerHeight());
})
You can find a detailed post here.
您可以在此处找到详细的帖子。
回答by Tom Roggero
- Using
scrollHeight
is not only buggy, but doesn't work when your container has a hardcoded height (which is probably most cases, since you wanna get contents height without doingcontainer.height()
itself) - @shazboticus-s-shazbot solution is good when you can mess around with the container height temporarily / have a hard-coded height.
- An alternative solution would be:
- 使用
scrollHeight
不仅有问题,而且当您的容器具有硬编码高度时不起作用(这可能是大多数情况,因为您想在不做container.height()
自己的情况下获得内容高度) - @shazboticus-s-shazbot 解决方案很好,当您可以暂时弄乱容器高度/具有硬编码高度时。
- 另一种解决方案是:
$('#outer')
// Get children in array format, as we'll be reducing them into a single number
.contents().toArray()
// Filter out text and comment nodes, only allowing tags
.filter(el => el.nodeType === 1)
// Sum up all the children individual heights
.reduce((accumulator, el) => $(el).outerHeight(true) + accumulator, 0);
Of course, this latter alternative only works when #outer
doesn't have immediate text childrens that take up space and you want to measure. Those are my 2 cents.
当然,后一种选择仅在#outer
没有占用空间的直接文本子项并且您想要测量时才有效。那是我的 2 美分。
- If you want to measure unwrapped text, I'd suggest modifying your DOM tree and having an inner
<div>
of which you can measure easily by doing$('#outer').children().height()
- 如果您想测量未包装的文本,我建议修改您的 DOM 树并拥有一个
<div>
可以通过执行轻松测量的内部$('#outer').children().height()