间隔为2的幂次的翻页js实现

间隔为2的幂次的翻页js实现

<html>
<head>
<style>
div {
 padding: 20px;
}
.page-links a.post-page-numbers {
    padding: 7px 10px;
    border: solid #6AA 2px;
    border-radius: 4px;
}
.page-links span.post-page-numbers {
    padding: 7px 10px;
    border: solid #88D 2px;
    border-radius: 20px;
}
</style>
</head>
<body>
</body>
<script>

function printPageHref(pi,gap) {
	document.write("<a href=\"https://www.malic.xyz/archives/4298/" + pi +"\" class=\"post-page-numbers\"><ruby> "+ pi + "<rt>" + gap + "</rt></ruby></a>");
}
function listPageNext(curr,total)
{
	page = curr+1;
	gap = 2;
	while(page < total ){
		printPageHref(page,"(+" + (page-curr) + ")");
		page = Math.min(page + gap,total);
		gap = gap * 2;
	}
	printPageHref(page,"(+" + (page-curr) + ")");
}

function listPagePrev(curr)
{
	page = curr-1;
	gap = 2;
	arr = new Array();
	while(1 < page) {
		arr.unshift(page);
		page = Math.max(page - gap,1);
		gap = gap * 2;
	}
	if(arr.indexOf(1) < 0) {
		arr.unshift(1);
	}
	for(k = 0;k<arr.length;k++){
		printPageHref(arr[k],"(-"+ (curr-arr[k]) + ")");
	}
}

totalPage = 21;

function main(currp){
	document.write("<div class=\"page-links\">");
	if(currp>1){listPagePrev(currp);}
	document.write( "<span class=\"post-page-numbers current\" aria-current=\"page\">" + currp + "</span>");
	if(currp<totalPage){listPageNext(currp,totalPage);}
	document.write("</div>");	
}


for(i=1;i<=totalPage;i++) {
main(i);
}

</script>
</html>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注