我找了一个寻呼机,我可以处理我网站的缩略图,但我没有找到任何东西,所以我必须自己编写。我编写了一个函数,可以根据AJAX请求为任何数据创建一个寻呼机:
function addPager(s, m, f, pager, current) {
if (!current) {current = 2;}
// m - element / page
// s - all elements
// f - ajax getter function's name like functionName(id, page)
// current - pressed button's index or p n f l as prev next first last
if (!pager) { // if the pager not appended
if (s%m == 0) { p = s/m; } else {p = parseInt(s/m);} // get the pages number on all elements / elementsonpage
$pager = $('<div class="pager clearfix"></div>') // pager holder box
for (i=0;i<=p;i++) // append all pages to the pager holder and bind them for click
{
$('<span>'+(i+1)+'</span>')
.bind("click", {page: i}, f)
.click(function(){
var ind = $(this).index();
addPager(s, m, f, $(this).parent("div"), ind); // on click call this function again
})
.appendTo($pager); // the append
}
$pager.find("span").eq(0).addClass("on"); // find first element and add on class because its just appended
$('<span class="mod">Next</span>').click(function(){ addPager(s, m, f, $(this).parent("div"), "n"); }).appendTo($pager); // prev, next, first, last buttons
$('<span class="mod">Prev</span>').click(function(){ addPager(s, m, f, $(this).parent("div"), "p"); }).prependTo($pager);
$('<span class="mod">Last</span>').click(function(){ addPager(s, m, f, $(this).parent("div"), "l"); }).appendTo($pager);
$('<span class="mod">First</span>').click(function(){ addPager(s, m, f, $(this).parent("div"), "f"); }).prependTo($pager);
$pager.children("span").eq(0).addClass("on"); // add on class for the now 1st element (which is the first button)
$pager.children("span").eq(1).addClass("disabled"); // disable the prev button
$pager.children("span:not(.mod)").each(function(index) {
if (((current-3) > (index+2)) || ((current+3) < (index+2))) { $(this).hide(); } else { $(this).show(); } // hide everything but the first 6 buttons (in case of 1000 pages i just show 1 .. 6)
if (current == (index+2)) { $(this).addClass("on"); } // add on class to the first element - its probably useless because of the previous addonclass
});
return($pager); // return it for the function which called. i append in the primary ajax function.
}
else { // if the pager is already appended to the site
if (s%m == 0) { p = s/m; } // calculate pages
else {p = parseInt(s/m);} // calculate pages
if (current == "n") {var i = $pager.find("span:not(.mod).on").index(); if ((i>=1) && (i<(p+2))) {addPager(s, m, f, pager, (i+1));}} // if current next goto next
else if (current == "p") {var i = $pager.find("span:not(.mod).on").index(); if ((i>2) && (i<=(p+2))) {addPager(s, m, f, pager, (i-1));}} // if current prev goto prev
else if (current == "f") { addPager(s, m, f, pager, 2); } // 1st
else if (current == "l") { addPager(s, m, f, pager, (p+2)); } // last
else { // if any other button pressed
$pager = pager;
$pager.find("span").removeClass("on disabled"); // remove on or disabled classes on each buttons
$pager.find("span:not(.mod)").each(function(index) { // find numbered items only (without the spec buttons like prev or last)
if (((current-3) > (index+2)) || ((current+3) < (index+2))) { $(this).hide(); } else { $(this).show(); } // hide all the buttons but the nearest ones
if (current == (index+2)) { $(this).addClass("on"); }
});
if (current == 2) { $pager.children("span").eq(0).addClass("on"); $pager.children("span").eq(1).addClass("disabled"); }// first button makes the prev diabled and first active
if (current == (p+2)) { $pager.children("span").eq(p+4).addClass("on"); $pager.children("span").eq(p+3).addClass("disabled"); } // last button makes the next diabled and last active
}
}
}
......可以像这样使用;
$.ajax {
...
addPager(allelementsnumber, elementsonpage, thisajaxfunctionsname);
$pager.appendTo(whereyouwanttoappendthis);
...
}
CC许可,如果您愿意,可以使用它,工作正常:)
当我多次打电话给addPager(param, param, param, param, param, param)
时,有这么多参数...是否会使用大量内存或类似内容?
如何更好或更快地完成此功能?
我为更简单的理解做了一些评论,但我认为这不是一个复杂的功能。
答案 0 :(得分:1)
最好创建一个具有属性的对象,然后传递它而不是多个参数。对象的每个属性都代表一个参数。您可以将其视为选项/配置对象。
然后,您可以将对象作为单个参数传递给函数,然后使用对象的属性。
这是一种更好的方法,因为从长远来看它提供了更好的可扩展性。
但是,我不确定使用许多参数会产生开销。在我看来,它只是难以阅读而且不具备可扩展性。