我試過找到一個可以理解的答案,但放棄了。用jQuery計算兒童的總寬度
爲了有dymnamic內容(如博客文章和圖片)在水平網站(就像thehorizontalway.com)必須在pixles設定一個固定的寬度爲體,對不對? 因爲你使用浮動元素,否則會打破和包裝頁面,這取決於瀏覽器的寬度。
編輯!這個具體的值可以是計算與jQuery,謝謝你:)在這個例子中,一個額外的值被添加到總大小,它可以用於浮動元素之前的內容。現在身體獲得動態寬度!
我最初的想法是讓jQuery的計算是爲我們: ( '各自崗位WIDTH' * '的帖子數')+ 250(用於額外的內容)
HTML代碼
<body style="width: ;"> <!-- Here we want a DYNAMIC value -->
<div id="container">
<div id="menu"></div> <!-- Example of extra content before the floats -->
<div class="post">Lorem</div>
<div class="post">Lorem</div>
<div class="post">Lorem</div> <!-- Floated elements that we want the sizes of -->
<div class="post">Lorem</div>
</div>
...
<!-- So if these posts were 300px exept the last that was 500px wide, then the BODY WIDTH should be (300+300+300+500) + 250 [#menu] = 1650px -->
Alconja的結果和答案
$(document).ready(function() {
var width = 0;
$('.post').each(function() {
width += $(this).outerWidth(true);
});
$('body').css('width', width + 250);
});
非常感謝!
太棒了,那正是我的意思。稍後我會編輯問題,以便更清楚地說明我在說什麼。就像你說的那樣,我只用第一個.post代碼,然後用.size()來計算剩餘的代碼 - 這使得它更容易製作水平滾動頁面,使得頁面的寬度變爲每頁動態的寬度:)需要添加一些無腳本超級體寬,並完成。 – elundmark 2009-06-19 01:44:52