JavaScript Paging code (the current page number is centered) _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

function Setpage (opt) {
if (!opt.pagedivid | | Opt.allpagenum < Opt.curpagenum | | Opt.allpagenum < opt.showpagenum) {return false};
var allpagenum = Opt.allpagenum; Total number of pages
var showpagenum = Opt.showpagenum; Number of pages displayed
var curpagenum = Opt.curpagenum; Current number of pages
var pagedivbox = document.getElementById (Opt.pagedivid);
Number of page numbers displayed on the left or right
var lrnum = Math.floor (SHOWPAGENUM/2);
if (curpagenum>1) {
var OA = document.createelement (' a ');
oa.href= ' #1 ';
oa.innerhtml = ' Home '
Pagedivbox.appendchild (OA);
}
if (curpagenum>1) {
var OA = document.createelement (' a ');
oa.href= ' # ' + (curpageNum-1);
oa.innerhtml = ' previous page '
Pagedivbox.appendchild (OA);
}
if (curpagenum<showpagenum-2 | | allpagenum = = showpagenum) {
for (Var i=1;i<=showpagenum;i++) {
var OA = document.createelement (' a ');
Oa.href = ' # ' +i;
if (curpagenum==i) {
oa.innerhtml = i;
}else{
oa.innerhtml = "[" + i + "]";
}
Pagedivbox.appendchild (OA);
}
}else{
The first page of the countdown process
if (allpagenum-curpagenum<lrnum && curpagenum = = allPageNum-1) {
for (Var i=1;i<=showpagenum;i++) {
Console.log ((Curpagenum-showpagenum + i));
var OA = document.createelement (' a ');
Oa.href = ' # ' + (Curpagenum-(showPageNum-1) + i);
if (Curpagenum = = (Curpagenum-(showPageNum-1) + i)) {
oa.innerhtml = (Curpagenum-(showPageNum-1) + i)
}else{
oa.innerhtml = ' [' + (Curpagenum-(showPageNum-1) + i) + '] '
}
Pagedivbox.appendchild (OA);
}
}
Processing of the last page
else if (allpagenum-curpagenum<lrnum && curpagenum = = allpagenum) {
for (Var i=1;i<=showpagenum;i++) {
Console.log ((Curpagenum-showpagenum + i));
var OA = document.createelement (' a ');
Oa.href = ' # ' + (curpagenum-showpagenum + i);
if (Curpagenum = = (Curpagenum-showpagenum + i)) {
oa.innerhtml = (Curpagenum-showpagenum + i)
}else{
oa.innerhtml = ' [' + (Curpagenum-showpagenum + i) + '] '
}
Pagedivbox.appendchild (OA);
}
}else{
for (Var i=1;i<=showpagenum;i++) {
var OA = document.createelement (' a ');
Oa.href = ' # ' + (Curpagenum-(showpagenum-lrnum) + i);
if (Curpagenum = = (Curpagenum-(showpagenum-lrnum) + i)) {
oa.innerhtml = (Curpagenum-(showpagenum-lrnum) + i)
}else{
oa.innerhtml = ' [' + (Curpagenum-(showpagenum-lrnum) + i) + '] '
}
Pagedivbox.appendchild (OA);
}
}
}
if (curpagenum<allpagenum) {
for (Var i=1;i<=2;i++) {
if (i==1) {
var OA = document.createelement (' a ');
oa.href= ' # ' + (parseint (curpagenum) +1);
oa.innerhtml = ' next page '
}else{
var OA = document.createelement (' a ');
oa.href= ' # ' +allpagenum;
oa.innerhtml = ' last '
}
Pagedivbox.appendchild (OA);
}
}
var OA = document.getElementsByTagName (' a ');
Add a click event to a page number
for (Var i=0;i<oa.length;i++) {
Oa[i].onclick = function () {
Number of the current point
var shref = this.getattribute (' href '). substring (1);
Empty page display
pagedivbox.innerhtml = ';
Setpage ({
Pagedivid: ' Page ',
Showpagenum:5,//display number
Allpagenum:10,//Total pages
Curpagenum:shref//Current page
})
}
}
}
Window.onload = function () {
Setpage ({
Pagedivid: ' Page ',
Showpagenum:5,//display number
Allpagenum:10,//Total pages
Curpagenum:1//Current page
})
}

Yesterday I saw the wonderful flavor of the classroom Paging video tutorial, today I refer to its ideas, I wrote down, and I added a ' show page number ' attribute ' showpagenum ';

make a summary of the following key areas:

1. Click on the current page number to be displayed in the center of the page number;
Show 3 page 5 page 7 page 9 page ... Wait a minute
To center the current page, you can introduce a formula
By dividing the number of page numbers by 2 and then rounding, you can get the number of page numbers that need to be explicitly left or right. This is useful for the following pagination.
var lrnum = Math.floor (SHOWPAGENUM/2);

2. Get page numbers
This.getattribute (' href ') uses it to get a relative path; this.href use it to get an absolute path.

Demo Online Demo http://demo.jb51.net/js/2012/js_page/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.