2
我想要計算"height: auto"
div的身高,然後將其應用於div。我已經做了這樣的:獲取所有孩子的身高並將其應用於父母
function center(divName) {
$(":animated").promise().done(function() {
var totalHeight = 0;
$(divName).children().each(function() {
alert(totalHeight);
totalHeight += $(this).height();
});
$(divName).css("height", totalHeight + "px");
});
}
center("#frame_01");
的HTML:
<div id="frame_01">
<h1>Text</h1>
<h2 id="next_frame_02">Continue</h2>
</div>
通知的alert(totalHeight);
,使用測試腳本,問題是,腳本返回第一個孩子0像素,但即時通訊第二個正確。
我首先想到這將是因爲它在動畫完成前開始計算,因此沒有獲得第一個孩子的高度,所以我申請了$(":animated").promise().done(function() {
,所以腳本在計算之前等待動畫完成。
但是這並沒有解決它,有什麼建議,爲什麼?
在此先感謝!
編輯:
什麼我試着去實現中心是一個div,但要做到這一點,我需要的div的高度。這是CSS:
#frame_01 {
text-align: center;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
高度設置與background-color: red
後:
我可能會錯過一些看起來很親切的東西顯然,但不是因爲'alert(totalHeight)''需要_after_' totalHeight + = $(this).height();'? – Matt
你是如何動畫的?通過'css3'或jQuery的'.animate()' – Jai
您在計算totalheight之前做出警報,因此對於第一個孩子,它顯示爲0。警報(totalHeight); –