How do I get the “auto” height of a DIV

南楼画角 提交于 2019-12-10 10:58:23

问题


So when I set a fixed height on a div with jquery, like $('div').height(200);, the value of $('div').height() is always 200. Even if the content from that div exceeds that height (I use overflow:hidden).

How can I get that true height of the DIV as if it would be in "auto" mode ?


回答1:


USE

.scrollHeight()

element.scrollHeight

with jquery try this

 $(selector)[0].scrollHeight

DESCRIPTION

An element's scrollHeight is a measurement of the height of an element's content including content not visible on the screen due to overflow.

Example

DEMO from Vega's Answer




回答2:


You mean hidden content height?

Vega - yes. The height of the content of the div, including the part that gets hidden by overflow

Just set to auto and get the .height and set it back to the fixed height.

var $el = $('#test');
var tmp = $el.css('height');

var actualHeight = $el.css('height', 'auto').height();

$el.css('height', tmp);
alert(actualHeight);

DEMO: http://jsfiddle.net/sKZfF/



来源:https://stackoverflow.com/questions/18133181/how-do-i-get-the-auto-height-of-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!