如何使用jQuery获取div的完整内容的高度?

时间:2022-07-31 21:10:02

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(),浏览器甚至都不识别它。溢出区域具有固定位置。

6 个解决方案

#1


119  

scrollHeight is a property of 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控制台打开的情况下运行)。

#2


30  

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($( '#外')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的值。我遇到了这个问题,制作了自己的滚动窗格。希望这有助于某人。

#3


26  

We can also use -

我们也可以用 -

$('#x').prop('scrollHeight') <!-- Height -->
$('#x').prop('scrollWidth')  <!-- Width -->

#4


5  

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是一个属性,而不是一个函数,如此处所述。如上所述,仅在IE8之后支持scrollHeight属性。如果在此之前需要它,可以暂时将CSS overflow和height设置为auto,这将使div获得所需的最大高度。然后获取高度,并将属性更改回之前的属性。

#5


1  

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。您可以保持其高度自动以正确填充正确高度的内容。

$('#x').ready( function(){
// alerts the height in pixels
alert($('#x').outerHeight());
})

You can find a detailed post here.

你可以在这里找到详细的帖子。

#6


1  

  1. 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 doing container.height() itself)
  2. 使用scrollHeight不仅有bug,而且当你的容器具有硬编码高度时也不起作用(这可能是大多数情况,因为你想要获得内容高度而不执行container.height()本身)
  3. @shazboticus-s-shazbot solution is good when you can mess around with the container height temporarily / have a hard-coded height.
  4. @ shazboticus-s-shazbot解决方案很好,当你可以暂时容纳容器高度/具有硬编码高度。
  5. An alternative solution would be:
  6. 另一种解决方案是:

$('#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美分。

  1. 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()
  2. 如果你想测量未包装的文本,我建议你修改你的DOM树并有一个内部的
    你可以通过$('#outer')轻松测量.child()。height()

#1


119  

scrollHeight is a property of 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控制台打开的情况下运行)。

#2


30  

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($( '#外')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的值。我遇到了这个问题,制作了自己的滚动窗格。希望这有助于某人。

#3


26  

We can also use -

我们也可以用 -

$('#x').prop('scrollHeight') <!-- Height -->
$('#x').prop('scrollWidth')  <!-- Width -->

#4


5  

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是一个属性,而不是一个函数,如此处所述。如上所述,仅在IE8之后支持scrollHeight属性。如果在此之前需要它,可以暂时将CSS overflow和height设置为auto,这将使div获得所需的最大高度。然后获取高度,并将属性更改回之前的属性。

#5


1  

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。您可以保持其高度自动以正确填充正确高度的内容。

$('#x').ready( function(){
// alerts the height in pixels
alert($('#x').outerHeight());
})

You can find a detailed post here.

你可以在这里找到详细的帖子。

#6


1  

  1. 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 doing container.height() itself)
  2. 使用scrollHeight不仅有bug,而且当你的容器具有硬编码高度时也不起作用(这可能是大多数情况,因为你想要获得内容高度而不执行container.height()本身)
  3. @shazboticus-s-shazbot solution is good when you can mess around with the container height temporarily / have a hard-coded height.
  4. @ shazboticus-s-shazbot解决方案很好,当你可以暂时容纳容器高度/具有硬编码高度。
  5. An alternative solution would be:
  6. 另一种解决方案是:

$('#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美分。

  1. 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()
  2. 如果你想测量未包装的文本,我建议你修改你的DOM树并有一个内部的
    你可以通过$('#outer')轻松测量.child()。height()