欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

javascript 支持页码格式的分页类_javascript技巧Jpage Demo - by funnyzak

程序员文章站 2022-03-14 20:23:57
...
前端时间因为项目需要,要对一个产品下所有的附属图片进行分页显示,没考虑ajax一张张请求,所以干脆一次性全部把图片out,然后在载入的时候进行分页,网上很多jquery,property等都实现了

但是他们插件的附属功能很多又不需要,而且没必要就为了这么一个功能区引用这个库,为速度考虑吧,当然你服务器好也行,但是其实这个效果不是必须的,但是觉的会常用,所以就包装成类了,供以后使用,也供有需要的人学习使用。

下面只是一个简单的demo,用的话自己可以在编辑页码样式,有默认的格式。不废话了,自己看吧,有注释使用说明。。。类(3kb)使用可以参照demo,不明白使用的可以留言。

以后会慢慢写一些项目开发中常遇到的问题,以及解决方案,和大家学习分享。
javascript 支持页码格式的分页类_javascript技巧Jpage Demo - by funnyzak
jpage.js

/* 排序工具 by Funny ZAk <silenceacegmail.com> 2009-8-23 
"调用方式 var zp = new zakPage(参数);zp.init(); 
参数以此为 源内容容器,分页结果容器,导航容器,页大小,单挑内容回调函数返回li对象,每个li的样式,选择后的样式没有则为null或"" 
,默认起始页数,格式化页码显示格式*/ 
function zakPage(id,pagec,navid,pagesize,lievt,ds,ss,idx,navp){ 
    this.obj = this; 
    this.pageid = id; 
    this.pagec = pagec; 
    this.navc = navid; 
    this.pagesize = pagesize || 7; 
    this.lievt = lievt || null; 
    this.rcount = 0; 
    this.pagecount = 0; 
    this.cpage = 1; 
    this.ds = ds; 
    if(this.ds == null){this.ds = "";} 
    this.ss = ss; 
    if(this.ss == null){this.ss = "";} 
    this.idx = idx; 
    this.navpre = navp || ["共{$pcount}页/{$rcount}条,当前第{cpage}页","首页","上一页","下一页","末页"]; 
    } 
zakPage.prototype = { 
    init:function(){ 
        document.getElementById(this.pageid).style.display = "none"; 
        this.rcount = document.getElementById(this.pageid).getElementsByTagName("li").length; 
        var residue = this.rcount%this.pagesize; 
        var nums = parseInt(this.rcount/this.pagesize); 
        this.pagecount = nums; 
        if(residue != 0){this.pagecount = nums+1;} 
        this.gopage(this.idx); 
        }, 
    guide:function(){ 
        var nav = document.getElementById(this.navc); 
        var np = this.navpre; 
        nav.innerHTML = np[0].replace("{$rcount}",this.rcount).replace("{$pcount}",this.pagecount).replace("{cpage}",this.cpage); 
        nav.appendChild(this.createa(np[1],1)); 
        nav.appendChild(this.createa(np[2],this.cpage-1)); 
        nav.appendChild(this.createa(np[3],this.cpage+1)); 
        nav.appendChild(this.createa(np[4],this.pagecount)); 
        }, 
    createa:function(html,index){ 
        var aa = document.createElement("a"); 
        aa.innerHTML = html; 
        var o = this.obj; 
        aa.onclick = function(){o.gopage(index);} 
        return aa; 
        }, 
    gopage:function(index){ 
        if(index>this.pagecount){index=1;} 
        if(index<=0){index = this.pagecount;} 
        this.cpage = index; 
        var ghtml = document.getElementById(this.pagec); 
        ghtml.innerHTML = ""; 
        var pages = (index-1)*this.pagesize; 
        var pagee = pages+this.pagesize; 
        if(pagee>this.rcount){pagee = this.rcount;} 
        for(var i=pages;i<pagee;i++){ 
                var lil = document.createElement("li"); 
                lil.className = this.ds; 
                lil.innerHTML = this.litem(i); 
                var sss = this.ss; 
                if(this.lievt !=null){ 
                    lil.lievt = this.lievt; 
                    lil.onclick = function(){ 
                        this.lievt(this); 
                        this.className = sss; 
                        } 
                    } 
                ghtml.appendChild(lil); 
            } 
         this.guide(); 
        }, 
    litem:function(index){ 
            return document.getElementById(this.pageid).getElementsByTagName("li")[index].innerHTML; 
        } 
    }


下面是完整的测试代码,js已经包括在内

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<style type="text/css"> 
body { 
    text-align:center; 
    padding:20px 0 0 0; 
    font-size:12px; 
} 
a { 
    color:#000; 
    font-size:12px; 
    cursor:pointer; 
} 
ul, li { 
    margin:0; 
    padding:0; 
    list-style:none; 
} 
.contain { 
    margin:0 auto; 
    width:825px; 
    text-align:left; 
    border:1px #000 solid; 
    padding:5px; 
    height:auto; 
} 
li.sd a,li.sd a:link,li.sd a:visited{background-color:#000; color:#FFF; font-size:13px;} 
</style> 
<title>Jpage Demo - by funnyzak</title> 
<script type="text/javascript" charset="gb2312"> 
/* 排序工具 by Funny ZAk <silenceacegmail.com> 2009-8-23 
"调用方式 var zp = new zakPage(参数);zp.init(); 
参数以此为 源内容容器,分页结果容器,导航容器,页大小,单挑内容回调函数返回li对象,每个li的样式,选择后的样式没有则为null或"" 
,默认起始页数,格式化页码显示格式*/ 
function zakPage(id,pagec,navid,pagesize,lievt,ds,ss,idx,navp){ 
    this.obj = this; 
    this.pageid = id; 
    this.pagec = pagec; 
    this.navc = navid; 
    this.pagesize = pagesize || 7; 
    this.lievt = lievt || null; 
    this.rcount = 0; 
    this.pagecount = 0; 
    this.cpage = 1; 
    this.ds = ds; 
    if(this.ds == null){this.ds = "";} 
    this.ss = ss; 
    if(this.ss == null){this.ss = "";} 
    this.idx = idx; 
    this.navpre = navp || ["共{$pcount}页/{$rcount}条,当前第{cpage}页","首页","上一页","下一页","末页"]; 
    } 
zakPage.prototype = { 
    init:function(){ 
        document.getElementById(this.pageid).style.display = "none"; 
        this.rcount = document.getElementById(this.pageid).getElementsByTagName("li").length; 
        var residue = this.rcount%this.pagesize; 
        var nums = parseInt(this.rcount/this.pagesize); 
        this.pagecount = nums; 
        if(residue != 0){this.pagecount = nums+1;} 
        this.gopage(this.idx); 
        }, 
    guide:function(){ 
        var nav = document.getElementById(this.navc); 
        var np = this.navpre; 
        nav.innerHTML = np[0].replace("{$rcount}",this.rcount).replace("{$pcount}",this.pagecount).replace("{cpage}",this.cpage); 
        nav.appendChild(this.createa(np[1],1)); 
        nav.appendChild(this.createa(np[2],this.cpage-1)); 
        nav.appendChild(this.createa(np[3],this.cpage+1)); 
        nav.appendChild(this.createa(np[4],this.pagecount)); 
        }, 
    createa:function(html,index){ 
        var aa = document.createElement("a"); 
        aa.innerHTML = html; 
        var o = this.obj; 
        aa.onclick = function(){o.gopage(index);} 
        return aa; 
        }, 
    gopage:function(index){ 
        if(index>this.pagecount){index=1;} 
        if(index<=0){index = this.pagecount;} 
        this.cpage = index; 
        var ghtml = document.getElementById(this.pagec); 
        ghtml.innerHTML = ""; 
        var pages = (index-1)*this.pagesize; 
        var pagee = pages+this.pagesize; 
        if(pagee>this.rcount){pagee = this.rcount;} 
        for(var i=pages;i<pagee;i++){ 
                var lil = document.createElement("li"); 
                lil.className = this.ds; 
                lil.innerHTML = this.litem(i); 
                var sss = this.ss; 
                if(this.lievt !=null){ 
                    lil.lievt = this.lievt; 
                    lil.onclick = function(){ 
                        this.lievt(this); 
                        this.className = sss; 
                        } 
                    } 
                ghtml.appendChild(lil); 
            } 
         this.guide(); 
        }, 
    litem:function(index){ 
            return document.getElementById(this.pageid).getElementsByTagName("li")[index].innerHTML; 
        } 
    } 

</script> 
</head> 
<body> 
<div> 
<ul id="jpage"> 
<li><a href="http://www.jb51.net/article/21316.htm" title="Php 构造函数construct的前下划线是双的_">Php 构造函数construct的前下划线是</a></li> 
<li><a href="http://www.jb51.net/article/21261.htm" title="PHP 读取文件内容代码(txt,js等)">PHP 读取文件内容代码(txt,js等)</a></li> 
<li><a href="http://www.jb51.net/article/21248.htm" title="PHP 用数组降低程序的时间复杂度">PHP 用数组降低程序的时间复杂度</a></li> 
<li><a href="http://www.jb51.net/article/21245.htm" title="PHP 柱状图实现代码">PHP 柱状图实现代码</a></li> 
<li><a href="http://www.jb51.net/article/21232.htm" title="Ajax+PHP边学边练 之五 图片处理">Ajax+PHP边学边练 之五 图片处理</a></li> 
<li><a href="http://www.jb51.net/article/21228.htm" title="PHPMyadmin 配置文件详解(配置)">PHPMyadmin 配置文件详解(配置)</a></li> 
<li><a href="http://www.jb51.net/article/21216.htm" title="又一个php 分页类实现代码">又一个php 分页类实现代码</a></li> 
<li><a href="http://www.jb51.net/article/21215.htm" title="php 无限分类的树类代码">php 无限分类的树类代码</a></li> 
<li><a href="http://www.jb51.net/article/21207.htm" title="Asp.net 文本框全选的实现">Asp.net 文本框全选的实现</a></li> 
<li><a href="http://www.jb51.net/article/21205.htm" title="php zip文件解压类代码">php zip文件解压类代码</a></li> 
li><a href="/photoshop/23037.html" target="_blank">各种男女围巾围法图文</a></li><li> 
<a href="/photoshop/23036.html" target="_blank">photoshop 鼠绘忧郁的少女头像</a></li> 
<li><a href="/photoshop/23035.html" target="_blank">Photoshop 一只可爱的卡通小蜜蜂</a></li> 
<li><a href="/photoshop/23034.html" target="_blank">Photoshop 调出美女质感的红紫色肤色</a></li> 
<li><a href="/photoshop/23033.html" target="_blank">Photoshop 古典的油画效果处理方法</a></li> 
<li><a href="/photoshop/22983.html" target="_blank">photoshop 利用滤镜及素材打造超酷的火焰字</a></li> 
<li><a href="/photoshop/22982.html" target="_blank">photoshop 鼠绘漂亮的熟睡公主</a></li> 
<li><a href="/photoshop/22981.html" target="_blank">Photoshop 华丽的金色宝石皇冠</a></li> 
<li><a href="/photoshop/22980.html" target="_blank">Photoshop 粉红色的人物写真图片</a></li> 
<li><a href="/photoshop/22979.html" target="_blank">Photoshop 漂亮的紫色人物签名效果</a></li> 
<li><a href="/photoshop/22978.html" target="_blank">Photoshop 偏暗的圣诞美女美白</a></li><li><a href="/photoshop/22954.html" target="_blank">Photoshop 蓝色梦幻的潮流壁纸制作</a></li><li><a href="/photoshop/22953.html" target="_blank">Photoshop调色教程 人物淡灰色质感肤色</a></li><li><a href="/photoshop/22947.html" target="_blank">Photoshop 漂亮的圣诞彩球制作</a></li><li><a href="/photoshop/22946.html" target="_blank">Photoshop 简单的光照字效果</a></li> 
<li><a href="http://www.jb51.net/tools/html-js.htm" target="_blank">HTML/JS互相转换工具-IE7兼容版</a></li> 
<li><a href="http://www.jb51.net/tools/cssyasuo.shtml" target="_blank">css 在线压缩工具</a></li> 
<li><a href="http://www.jb51.net/csstidy/css_optimiser.php?lang=zh" target="_blank">CSS整形与最佳化工具[压缩] </a></li> 
<li><a href="http://www.jb51.net/tools/jsmin/index.htm" target="_blank">JS Minifier js压缩</a></li> 
<li><a href="http://www.jb51.net/tools/packer.htm" target="_blank">packer_至强的javascript在线加密工具</a></li> 
<li><a href="http://www.jb51.net/tools/bihe/index.htm" target="_blank">XHTML 代码嵌套查看工具[标签闭合]</a></li> 
<li><a href="http://www.jb51.net/tools/jinzhi.htm" target="_blank">脚本之家 在线进制转换 可以实现各类进制间</a></li> 
<li><a href="http://www.jb51.net/tools/js_Debug.htm" target="_blank">在线JS脚本校验器检测js错误 </a></li> 
<li><a href="http://www.jb51.net/tools/htmlto.htm" target="_blank">将html转换为php,javascript和asp的在线工具</a></li> 
<li><a href="http://www.jb51.net/tools/zhengze.html" target="_blank" rel="external">正则表达式30分钟入门教程</a></li> 
<li><a href="http://www.jb51.net/article/18526.htm" target="_blank" rel="external">正则表达式 基础资料</a></li> 
<li><a href="http://www.jb51.net/w3school/js/index.asp.htm" target="_blank">javascript 在线参考手册</a></li> 
<li><a href="http://www.jb51.net/shouce/vbscript/index.html" target="_blank">vbscript微软官方参考手册</a></li> 
<li><a href="http://www.jb51.net/shouce/jquery/jQuery_study.html" target="_blank">jQuery入门指南教程</a></li> 
<li><a href="http://www.jb51.net/w3school/asp/index.asp.htm" target="_blank">asp 学习在线手册</a></li> 
<li><a href="http://www.jb51.net/shouce/php5/index.htm" title="photoshop教程" target="_blank">php5最新中文参考手册</a></li> 
<li><a href="http://img.jb51.net/shouce/mysql/index.html" title="mysql 5.1 参考手册" target="_blank">mysql</a></li> 
<li><a href="http://www.jb51.net/w3school/sql/index.asp.htm" title="sql数据库学习" target="_blank">sql数据库学习</a></li> 
<li><a href="http://www.jb51.net/adtest/index.htm" rel="external">常用广告代码</a></li> 
<li><a href="http://www.jb51.net/tools/baidu/index.asp" target="_blank">百度近日收录查询</a></li> 
<li><a href="http://tools.jb51.net/alexa/index.asp" target="_blank">alexa排名查询</a></li> 
<li><a href="http://tools.jb51.net/whois.asp" target="_blank">IP/域名WHOIS</a></li> 
<li><a href="http://www.jb51.net/codes/list37_1.html" target="_blank">网页编辑器</a></li> 
<li><a href="http://www.jb51.net/tools/qq.html" target="_blank">QQ在线强制聊天工具 加强修正版</a></li> 
<li><a href="http://www.jb51.net/tools/xunleijm.htm" target="_blank">迅雷 快车专用链接加密解密js代码</a></li> 
<li><a href="http://www.jb51.net/tools/fanjianzhi.htm" target="_blank">在线繁简体字转换</a></li> 
<li><a href="/shouce/html/index.html" target="_blank">html中文完全手册</a></li> 
<li><a href="http://www.jb51.net//tools/qvod.html" target="_blank">qvod解密工具 马克思</a></li> 
</ul> 
<ul id="pagec"> 
</ul> 
</div> 
<div id="navs"></div> 
<script type="text/javascript"> 
function bb(a){alert(a.innerHTML);} 
var arr = ["{$rcount}record,total{$pcount}page currenpage{cpage} "," indexpage"," prevpage "," nextpage "," lastpage "]; 
var zp = new zakPage("jpage","pagec","navs",15,bb,null,null,4,null); //此为默认页码格式 
//var zp = new zakPage("jpage","pagec","navs",15,bb,null,null,4,arr); //自定义页码格式 
zp.init(); 
</script> 
</body> 
</html>