是否有可能時,它的max-height
設置爲0獲得的元素ATTR height
CSS值?的Javascript/jQuery的:得到高度時,最大高度設定爲0
我需要這個知道怎麼元素的多少像素的onclick顯示,自每一個元素具有不同的高度,與我想從CSS文件中獲得高度值瀏覽器的大小改變。
是否有可能時,它的max-height
設置爲0獲得的元素ATTR height
CSS值?的Javascript/jQuery的:得到高度時,最大高度設定爲0
我需要這個知道怎麼元素的多少像素的onclick顯示,自每一個元素具有不同的高度,與我想從CSS文件中獲得高度值瀏覽器的大小改變。
我的第一個問題就是要你爲什麼要使用max-height: 0;
有沒有別的東西,你可以使用嗎?像hide()
show()
其使用display: none
我唯一可以的是,除去最大高度的CSS得到高度,然後重新應用最大高度件事:
$('.div').css('max-height', 'none')
console.log($('.div').height())
$('.div').css('max-height', '0')
這應該發生足夠快你不會看到的元素,但它可能是明智的去除max-height
之前將其隱藏:
$('.div').hide()
$('.div').css('max-height', 'none')
console.log($('.div').height())
$('.div').css('max-height', '0')
$('.div').show()
保留最大高度離CSS。緩存變量中的高度。然後再次將max-height
添加到0。
$(function(){
var divHeight = $('.div').height();
$('.div').css('max-height', '0');
alert(divHeight);
});
$('.div').each(function(){
var t=$(this); // Cache jQuery reference
t.css('max-height','none'); // Temporarily override max-height
t.data('height',t.height()); // Store height in data
t.css('max-height',''); // Remove our max-height override
});
alert($('.div').data('height'));
的jsfiddle:http://jsfiddle.net/AqAJc/7/
你可以得到一個元素的高度,如果你通過scrollHeight屬性屬性具有最大高度= 0。如果你設置最小重量,但它不是高度,它會起作用。
HTML:
<div>
Here you have a normal div
<div id="toggable">
Something hidden is in here, but we can still get the height !
<div class="other-content">
Something else here
</div>
</div>
</div>
JS:
alert(document.getElementById('toggable').scrollHeight)
CSS: #toggable { 最大高度:0; 溢出:隱藏; }
.other-content {
min-height: 100px;
}
演示:https://jsfiddle.net/asywL84u/2/
參考:https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight
這聽起來像你隱藏和顯示基於事件的元素。不要設置max-height屬性,而要查看'display:none;'或者jQuery的hide()和show()方法。 – cfs
他可能使用的最大高度,所以他可以過渡元素進入視野的onClick。你不能從display:none轉換到display:any。 – mcrider