HTML コンテンツをdiv
. コードは、このページにある jQuery rain サイトからのものです。
http://www.jqueryrain.com/?HtS47Rzc
このコードの目的は、大量の HTML を取得し、最上位の子要素をスキャンして、目的の高さ (例 ) に収まる子要素のグループごとにページ オブジェクトを作成すること400px
です。すべてのページが作成されると、各ページは新しいdiv
. 私が抱えている問題は、ページにレンダリングされると、計算されたページが目的の高さに近くないことです。そのため、各ページに含まれる の下部にきちんとぶつかるが、それを超えない素敵なテキスト ブロックがある代わりにdiv
、一部のページのテキストは目的のページの下部よりはるかに不足しており、一部のページはページの下部を超えています。div
各ページが でラップされた後にページ配列をスキャンし、コンテナdiv
を最大に設定するコードを追加したため、実際にはページの下部を超えなくなりましたdiv
高さが見つかりました。
私が考えていたのは、 のラッピングがdiv
差異を引き起こしているということだったので、マージンとパディングを に設定する CSS ルールを明示的に追加しました0px
。それは効果がありませんでした。ページの高さの計算が正しく機能するようにコードを調整する方法を教えてもらえますか?
更新:div
ページを保持する CSS とそれを含む DIV を示しています。
.example{
background:#FFF;
width:410px;
border:1px #000 solid;
margin:20px auto;
padding:15px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px
}
div
すべてのページには「ページ」のクラスがあります。
#content .page {
position:absolute;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
top:0px;
}
これが私のJavascript変更コードです。関数によって処理されるオブジェクトdiv
は、ページ分割されたコンテンツを表示するメインです。
(function ($) {
$.fn.extend({
MyPagination: function (options) {
var defaults = {
height: 400,
fadeSpeed: 400
};
var options = $.extend(defaults, options);
//Creating a reference to the object
var objContent = $(this);
// The array of pages we will build.
var fullPages = new Array();
// The array of elements for each page, used during pagination calculations.
var pageElements = new Array();
// The height for each page, reset after each page is built.
var height = 0;
var lastPage = 1;
var paginatePages;
// initialization function
init = function ()
{
// Build the array of pages by creating a new page when the sum of the child elements
// height values for each page exceeds the desired page height.
//
// NOTE: This is only an approximation. Haven't figured out why yet. When the
// operation is done there is large variance in the DIV height away from our desired
// height, many of them larger than our desired height.
objContent.children().each(function (i)
{
// Some browsers don't support clientHeight. In those cases, use offsetHeight.
var childHeight = this.clientHeight == 0 ? this.offsetHeight : this.clientHeight;
// If the height of all the children in the page elements array exceeds the desired
// page height, start a new page.
if (height + childHeight > options.height)
{
// Start a new page.
fullPages.push(pageElements);
// Reset the page elements array by initializing it to a new array.
pageElements = new Array();
// Reset the page height accumulatore. for the next page.
height = 0;
}
// Accumulate the child element's height into the height aggregator variable.
height += childHeight;
// Add the child element to the child elements array for the page currently being built.
pageElements.push(this);
});
if (height > 0) {
fullPages.push(pageElements);
}
// wrapping each full page
// $(fullPages).wrap("<div class='page'></div>");
// Wrapping each full page with a DIV. Give the DIV an ID that contains the page number.
$(fullPages).wrap(
function (ndx) {
return "<div class='page' name='pages' id='page_" + (ndx + 1) + "'></div>"
});
// Find the DIV with the maximum height.
var maxDivHeight = 0;
for (var ndx = 1; ndx <= fullPages.length; ndx++) {
var pageN = document.getElementById('page_' + ndx);
// Some browsers don't support clientHeight. In those cases, use offsetHeight.
var divHeight = pageN.clientHeight == 0 ? pageN.offsetHeight : pageN.clientHeight;
if (divHeight > maxDivHeight)
maxDivHeight = divHeight;
}
// Set the height of the content DIV to the maximum height we found plus a little padding.
objContent.height(maxDivHeight);
// hiding all wrapped pages
objContent.children().hide();
// making collection of pages for pagination
paginatePages = objContent.children();
// show first page
showPage(lastPage);
// draw controls
showPagination($(paginatePages).length);
};
// update counter function
updateCounter = function (i) {
$('#page_number').html(i);
};
// show page function
showPage = function (page) {
i = page - 1;
if (paginatePages[i]) {
// hiding old page, display new one
$(paginatePages[lastPage]).fadeOut(options.fadeSpeed);
lastPage = i;
$(paginatePages[lastPage]).fadeIn(options.fadeSpeed);
// and updating counter
updateCounter(page);
}
};
// perform initialization
init();
}
});
})(jQuery);