中文字幕在线直播,成人免费图片免费观看,国内精品国语自产拍在线观看,国产欧美精品区一区二区三区

增強(qiáng)網(wǎng)站用戶(hù)體驗(yàn)的幾種JS分頁(yè)代碼
時(shí)間:2011年05月31日 內(nèi)容來(lái)源: 瀏覽量:23

網(wǎng)頁(yè)設(shè)計(jì)中,要考慮的方面很多,當(dāng)然,大體來(lái)說(shuō)就是兩類(lèi):代碼+UI,簡(jiǎn)潔的代碼加上美觀的UI,是一個(gè)網(wǎng)站理想的選擇,一個(gè)網(wǎng)站設(shè)計(jì)成功與否,很多時(shí)候要從細(xì)節(jié)入手,關(guān)注細(xì)節(jié),才能關(guān)注用戶(hù)體驗(yàn),用戶(hù)體驗(yàn)的友好,才能說(shuō)明網(wǎng)站的設(shè)計(jì)是成功的,下面分享7種JS腳本分頁(yè)代碼,讓細(xì)節(jié)到從內(nèi)容頁(yè)面的分頁(yè)上來(lái),高手可以飄過(guò),這是寫(xiě)給不懂JS的朋友看的。

<script language="JavaScript">
<!--
/*

7種分頁(yè)腳本定義
=================================

Example
----------------------
var pg = new showPages('pg');
pg.pageCount = 12; //定義總頁(yè)數(shù)(必要)
pg.argName = 'p'; //定義參數(shù)名(可選,缺省為page)
pg.printHtml(); //顯示頁(yè)數(shù)

Supported in Internet Explorer, Mozilla Firefox
*/

function showPages(name) { //初始化屬性
this.name = name; //對(duì)象名稱(chēng)
this.page = 1; //當(dāng)前頁(yè)數(shù)
this.pageCount = 1; //總頁(yè)數(shù)
this.argName = 'page'; //參數(shù)名
this.showTimes = 1; //打印次數(shù)
}

showPages.prototype.getPage = function(){ //叢url獲得當(dāng)前頁(yè)數(shù),如果變量重復(fù)只獲取最后一個(gè)
var args = location.search;
var reg = new RegExp('[\?&]?' + this.argName + '=([^&]*)[&$]?', 'gi');
var chk = args.match(reg);
this.page = RegExp.$1;
}
showPages.prototype.checkPages = function(){ //進(jìn)行當(dāng)前頁(yè)數(shù)和總頁(yè)數(shù)的驗(yàn)證
if (isNaN(parseInt(this.page))) this.page = 1;
if (isNaN(parseInt(this.pageCount))) this.pageCount = 1;
if (this.page < 1) this.page = 1;
if (this.pageCount < 1) this.pageCount = 1;
if (this.page > this.pageCount) this.page = this.pageCount;
this.page = parseInt(this.page);
this.pageCount = parseInt(this.pageCount);
}
showPages.prototype.createHtml = function(mode){ //生成html代碼
var strHtml = '', prevPage = this.page - 1, nextPage = this.page + 1;
if (mode == '' || typeof(mode) == 'undefined') mode = 0;
switch (mode) {
case 0 : //模式1 (頁(yè)數(shù),首頁(yè),前頁(yè),后頁(yè),尾頁(yè))
strHtml += '<span class="count">Pages: ' + this.page + ' / ' + this.pageCount + '</span>';
strHtml += '<span class="number">';
if (prevPage < 1) {
strHtml += '<span title="First Page">&#171;</span>';
strHtml += '<span title="Prev Page">&#139;</span>';
} else {
strHtml += '<span title="First Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(1);">&#171;</a></span>';
strHtml += '<span title="Prev Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + prevPage + ');">&#139;</a></span>';
}
for (var i = 1; i <= this.pageCount; i++) {
if (i > 0) {
if (i == this.page) {
strHtml += '<span title="Page ' + i + '">[' + i + ']</span>';
} else {
strHtml += '<span title="Page ' + i + '"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + i + ');">[' + i + ']</a></span>';
}
}
}
if (nextPage > this.pageCount) {
strHtml += '<span title="Next Page">&#155;</span>';
strHtml += '<span title="Last Page">&#187;</span>';
} else {
strHtml += '<span title="Next Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + nextPage + ');">&#155;</a></span>';
strHtml += '<span title="Last Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + this.pageCount + ');">&#187;</a></span>';
}
strHtml += '</span><br />';
break;
case 1 : //模式1 (10頁(yè)縮略,首頁(yè),前頁(yè),后頁(yè),尾頁(yè))
strHtml += '<span class="count">Pages: ' + this.page + ' / ' + this.pageCount + '</span>';
strHtml += '<span class="number">';
if (prevPage < 1) {
strHtml += '<span title="First Page">&#171;</span>';
strHtml += '<span title="Prev Page">&#139;</span>';
} else {
strHtml += '<span title="First Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(1);">&#171;</a></span>';
strHtml += '<span title="Prev Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + prevPage + ');">&#139;</a></span>';
}
if (this.page % 10 ==0) {
var startPage = this.page - 9;
} else {
var startPage = this.page - this.page % 10 + 1;
}
if (startPage > 10) strHtml += '<span title="Prev 10 Pages"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + (startPage - 1) + ');">...</a></span>';
for (var i = startPage; i < startPage + 10; i++) {
if (i > this.pageCount) break;
if (i == this.page) {
strHtml += '<span title="Page ' + i + '">[' + i + ']</span>';
} else {
strHtml += '<span title="Page ' + i + '"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + i + ');">[' + i + ']</a></span>';
}
}
if (this.pageCount >= startPage + 10) strHtml += '<span title="Next 10 Pages"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + (startPage + 10) + ');">...</a></span>';
if (nextPage > this.pageCount) {
strHtml += '<span title="Next Page">&#155;</span>';
strHtml += '<span title="Last Page">&#187;</span>';
} else {
strHtml += '<span title="Next Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + nextPage + ');">&#155;</a></span>';
strHtml += '<span title="Last Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + this.pageCount + ');">&#187;</a></span>';
}
strHtml += '</span><br />';
break;
case 2 : //模式2 (前后縮略,頁(yè)數(shù),首頁(yè),前頁(yè),后頁(yè),尾頁(yè))
strHtml += '<span class="count">Pages: ' + this.page + ' / ' + this.pageCount + '</span>';
strHtml += '<span class="number">';
if (prevPage < 1) {
strHtml += '<span title="First Page">&#171;</span>';
strHtml += '<span title="Prev Page">&#139;</span>';
} else {
strHtml += '<span title="First Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(1);">&#171;</a></span>';
strHtml += '<span title="Prev Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + prevPage + ');">&#139;</a></span>';
}
if (this.page != 1) strHtml += '<span title="Page 1"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(1);">[1]</a></span>';
if (this.page >= 5) strHtml += '<span>...</span>';
if (this.pageCount > this.page + 2) {
var endPage = this.page + 2;
} else {
var endPage = this.pageCount;
}
for (var i = this.page - 2; i <= endPage; i++) {
if (i > 0) {
if (i == this.page) {
strHtml += '<span title="Page ' + i + '">[' + i + ']</span>';
} else {
if (i != 1 && i != this.pageCount) {
strHtml += '<span title="Page ' + i + '"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + i + ');">[' + i + ']</a></span>';
}
}
}
}
if (this.page + 3 < this.pageCount) strHtml += '<span>...</span>';
if (this.page != this.pageCount) strHtml += '<span title="Page ' + this.pageCount + '"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + this.pageCount + ');">[' + this.pageCount + ']</a></span>';
if (nextPage > this.pageCount) {
strHtml += '<span title="Next Page">&#155;</span>';
strHtml += '<span title="Last Page">&#187;</span>';
} else {
strHtml += '<span title="Next Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + nextPage + ');">&#155;</a></span>';
strHtml += '<span title="Last Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + this.pageCount + ');">&#187;</a></span>';
}
strHtml += '</span><br />';
break;
case 3 : //模式3 (箭頭樣式,首頁(yè),前頁(yè),后頁(yè),尾頁(yè)) (only IE)
strHtml += '<span class="count">Pages: ' + this.page + ' / ' + this.pageCount + '</span>';
strHtml += '<span class="arrow">';
if (prevPage < 1) {
strHtml += '<span title="First Page">9</span>';
strHtml += '<span title="Prev Page">7</span>';
} else {
strHtml += '<span title="First Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(1);">9</a></span>';
strHtml += '<span title="Prev Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + prevPage + ');">7</a></span>';
}
if (nextPage > this.pageCount) {
strHtml += '<span title="Next Page">8</span>';
strHtml += '<span title="Last Page">:</span>';
} else {
strHtml += '<span title="Next Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + nextPage + ');">8</a></span>';
strHtml += '<span title="Last Page"><a href=http://www.hunuo.com/Design/Pages/"javascript:' + this.name + '.toPage(' + this.pageCount + ');">:</a></span>';
}
strHtml += '</span><br />';
break;
case 4 : //模式4 (下拉框)
if (this.pageCount < 1) {
strHtml += '<select name="toPage" disabled>';
strHtml += '<option value=http://www.hunuo.com/Design/Pages/"0">No Pages</option>';
} else {
var chkSelect;
strHtml += '<select name="toPage" onchange="' + this.name + '.toPage(this);">';
for (var i = 1; i <= this.pageCount; i++) {
if (this.page == i) chkSelect=' selected="selected"';
else chkSelect='';
strHtml += '<option value=http://www.hunuo.com/Design/Pages/"' + i + '"' + chkSelect + '>Pages: ' + i + ' / ' + this.pageCount + '</option>';
}
}
strHtml += '</select>';
break;
case 5 : //模式5 (輸入框)
strHtml += '<span class="input">';
if (this.pageCount < 1) {
strHtml += '<input type="text" name="toPage" value=http://www.hunuo.com/Design/Pages/"No Pages" class="itext" disabled="disabled">';
strHtml += '<input type="button" name="go" value=http://www.hunuo.com/Design/Pages/"GO" class="ibutton" disabled="disabled"></option>';
} else {
strHtml += '<input type="text" value=http://www.hunuo.com/Design/Pages/"Input Page:" class="ititle" readonly="readonly">';
strHtml += '<input type="text" id="pageInput' + this.showTimes + '" value=http://www.hunuo.com/Design/Pages/"' + this.page + '" class="itext" title="Input page" onkeypress="return ' + this.name + '.formatInputPage(event);" onfocus="this.select()">';
strHtml += '<input type="text" value=http://www.hunuo.com/Design/Pages/" /' + this.pageCount + '" class="icount" readonly="readonly">';
strHtml += '<input type="button" name="go" value=http://www.hunuo.com/Design/Pages/"GO" class="ibutton" onclick="' + this.name + '.toPage(document.getElementById(\'pageInput' + this.showTimes + '\').value);"></option>';
}
strHtml += '</span>';
break;
default :
strHtml = 'Javascript showPage Error: not find mode ' + mode;
break;
}
return strHtml;
}
showPages.prototype.createUrl = function (page) { //生成頁(yè)面跳轉(zhuǎn)url
if (isNaN(parseInt(page))) page = 1;
if (page < 1) page = 1;
if (page > this.pageCount) page = this.pageCount;
var url = location.protocol + '//' + location.host + location.pathname;
var args = location.search;
var reg = new RegExp('([\?&]?)' + this.argName + '=[^&]*[&$]?', 'gi');
args = args.replace(reg,'$1');
if (args == '' || args == null) {
args += '?' + this.argName + '=' + page;
} else if (args.substr(args.length - 1,1) == '?' || args.substr(args.length - 1,1) == '&') {
args += this.argName + '=' + page;
} else {
args += '&' + this.argName + '=' + page;
}
return url + args;
}
showPages.prototype.toPage = function(page){ //頁(yè)面跳轉(zhuǎn)
var turnTo = 1;
if (typeof(page) == 'object') {
turnTo = page.options[page.selectedIndex].value;
} else {
turnTo = page;
}
self.location.href = http://www.hunuo.com/Design/Pages/this.createUrl(turnTo);
}
showPages.prototype.printHtml = function(mode){ //顯示html代碼
this.getPage();
this.checkPages();
this.showTimes += 1;
document.write('<div id="pages_' + this.name + '_' + this.showTimes + '" class="pages"></div>');
document.getElementById('pages_' + this.name + '_' + this.showTimes).innerHTML = this.createHtml(mode);

}
showPages.prototype.formatInputPage = function(e){ //限定輸入頁(yè)數(shù)格式
var ie = navigator.appName=="Microsoft Internet Explorer"?true:false;
if(!ie) var key = e.which;
else var key = event.keyCode;
if (key == 8 || key == 46 || (key >= 48 && key <= 57)) return true;
return false;
}
//-->
</script>

以上是定義,下面才是真正的調(diào)用 。

<script language="JavaScript">
<!--
var pg = new showPages('pg');
pg.pageCount =12; // 定義總頁(yè)數(shù)(必要)
//pg.argName = 'p'; // 定義參數(shù)名(可選,默認(rèn)為page)

document.write('<br>Show Times: ' + pg.showTimes + ', Mood Default');
pg.printHtml();
document.write('<br>Show Times: ' + pg.showTimes + ', Mood 0');
pg.printHtml(0);
document.write('<br>Show Times: ' + pg.showTimes + ', Mood 1');
pg.printHtml(1);
document.write('<br>Show Times: ' + pg.showTimes + ', Mood 2');
pg.printHtml(2);
document.write('<br>Show Times: ' + pg.showTimes + ', Mood 3 (only IE)');
pg.printHtml(3);
document.write('<br>Show Times: ' + pg.showTimes + ', Mood 4');
pg.printHtml(4);
document.write('<br>Show Times: ' + pg.showTimes + ', Mood 5');
pg.printHtml(5);
//-->
</script>

截個(gè)圖來(lái)看看效果吧

當(dāng)然這是原始的,沒(méi)定義過(guò)頁(yè)面字體的版本,在實(shí)際使用時(shí),可以根據(jù)自己網(wǎng)站的主題通過(guò)CSS定義頁(yè)面的字體,邊距屬性,使它看起來(lái)更美觀,更適合你的網(wǎng)站。