Aktualizacja:
podstawie innych odpowiedzi i dużo badań online wymyśliłem połączenie wszystkiego w jednej funkcji. Sprawdź jsfiddle tutaj: https://jsfiddle.net/oriadam/d01ap7r6/3/
// input a jQuery element
// return true for elements with auto height (90-100% is considered auto as well)
// return false for elements with fixed height
function is_height_auto($e) {
var e = $e[0],
// check fixed style:
chk = function(value) {
return /\d/.test(value) && !/^(100|9\d)\%/.test(value);
};
// start from the first, easiest, inline styles
if (chk(e.style.height)) {
// console.log('fixed for having style', e.style.height)
return false;
}
// start from the first, easiest, inline styles
var overflow = getComputedStyle(e)['overflow'];
if (overflow == 'scroll' || overflow == 'auto' || (e.tagName == 'BODY' && overflow == 'visible')) {
// console.log('auto for having overflow or is body', getComputedStyle(e)['overflow'], e.tagName);
return true;
}
// deprecated chrome way - check each rule that applies to the element
if (typeof getMatchedCSSRules == 'function') {
var i, MatchedCSSRules = getMatchedCSSRules(e) || [];
for (i = MatchedCSSRules.length; i; i--) {
if (MatchedCSSRules[i - 1].style.height) {
// console.log('found height at MatchedCSSRules[' + (i - 1) + ']: ', MatchedCSSRules[i - 1], ' All matches: ', MatchedCSSRules)
return !chk(MatchedCSSRules[i - 1].style.height);
}
}
}
// append something, see if height was changed, remove the something
var originalHeight = $e.height(),
$ghost = jQuery('<b style="display:block;height:1px;width:1px;padding:0;margin:0;">').appendTo($e),
newHeight = $e.height();
$ghost.remove(); // cleanup
// console.log('Using a ghost got ',newHeight > originalHeight,' originalHeight=' + originalHeight + ' newHeight=' + newHeight)
return newHeight > originalHeight;
} //is_height_auto()
** metoda elementów Duch wyjaśnił (poprzednia odpowiedź): **
Greg Pettit miał całkiem dobrą odpowiedź in his blog, tutaj jest główną ideą:
Co jest wyjątkowego w przypadku wysokości automatycznej? Cóż, fakt, że pozwala dynamicznie zmieniać wysokość, oczywiście!
- Clone element
- Umieścić go w widoczności: ukryte i position: absolute
- usunąć jego zawartość
- Zobacz czy zmienił wysokość (powinna ona wynosić około 0 teraz).
Cleanup
var isAutoHeight = function (element) {// dokonać poczekalnia dla całej naszej pracy. $ ('body'). Append ('');
// assume false by default
var autoHeight = false;
// clone the div and move it; get its height
var clone = element.clone();
clone.appendTo('#stage');
var initialHeight = clone.height();
// destroy all the content and compare height
clone.html('');
var currentHeight = clone.height();
if (currentHeight < initialHeight) {
autoHeight = true;
}
// get that clone and its smelly duplicate ID out of the DOM!
clone.remove();
// do the same for the stage
$('#stage').remove();
return autoHeight;
};
To działa, ale tylko wtedy, gdy jest stosowana w stylu inline, przez 'style' atrybutu :( –
Sprawdź wtyczki – pvnarula
dzięki! Skończyło się na klonowanie element w tle następnie porównano wysokości –