js原生分页插件,易使用易定制

<!doctype html>
<html>
    <head>
        <meta charset='utf-8'>
    </head>
    <body>

        <div id="pagesDom" class="pagesDom"></div>
        <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
        <script>
            $(function() {
                $('.pagesDom').createPage(function(n) {
                    console.log("当前页码:" + n)
                }, {
                    pageCount: 200,
                });
            });
            
            var style = document.createElement('style');
            style.type = 'text/css';
            style.rel = 'stylesheet';
            var code=".pagesDom a{text-decoration:none}.pagesDom .table2{border:#c8c8c8 solid;border-width:1px 0 0 1px;background:#f3f0f0;margin-top:25px}.pagesDom .td0{border:#c8c8c8 solid;border-width:0 0 1px 0}.pagesDom .td2{border:#c8c8c8 solid;border-width:0 1px 1px 0}.pagesDom{width:1000px;margin:0 auto;text-align:center}.pagesDom .pagesDom1{font-size:17px;float:left;margin-top:20px}.pagesDom .pagesDom2{float:right}.pagesDom .pagesDom2 ul{padding-left:0;list-style:none;text-align:center}.pagesDom .pagesDom2 li{display:inline}.pagesDom .pagesDom2 li input{width:26px;text-align:center;height:20px;border:0}.pagesDom .pagesDom2 a{font-size:16px;font-weight:normal;display:inline-block;padding:5px;padding-top:0;color:black;border:1px solid #ddd;background-color:#fff}.pagesDom .pagesDom2 a:hover{background-color:#eee}.pagesDom .ban{opacity:.4}";
            try{
                //for Chrome Firefox Opera Safari
                style .appendChild(document.createTextNode(code));
            }catch(ex){
                //for IE
                style.styleSheet.cssText = code;
            }
            var head = document.getElementsByTagName('head')[0];
            head.appendChild(style);
    
            $('.pagesDom').append('<div id="pagesDom1" class="pagesDom1"></div>\
                <div id="pagesDom2" class="pagesDom2">\
                    <ul>\
                        <li><a href="###" id="firstPage" οnclick="firstPage()">首页</a></li>\
                        <li><a href="###" id="prePage" οnclick="prePage()">上一页</a></li>\
                        <li><a href="###" id="nextPage" οnclick="nextPage()">下一页</a></li>\
                        <li><a href="###" id="lastPage" οnclick="lastPage()">尾页</a></li>\
                        <li><input type="text" id="jumpWhere" />\
                        </li>\
                        <li><a href="###" id="jumpPage" οnclick="jumpPage()">跳转</a></li>\
                    </ul>\
                </div>')
            //绑定成jQuery插件
            $.fn.createPage = function(fn, count) {
                goPage(count.pageCount, currentPage, fn)
            }
            var pageSize = 0; //每页显示行数
            var currentPage_ = 1; //当前页全局变量,用于跳转时判断是否在相同页,在就不跳,否则跳转。
            var totalPage, totals = 200,currentPage = 1,fns; //总页数
            function goPage(tal, pno, fn) {
                if (fn) {
                    fns = fn
                }
                var num = totals = tal; //表格所有行数(所有记录数)
                pageSize = 10; //每页显示行数
                //总共分几页 
                if (num / pageSize > parseInt(num / pageSize)) {
                    totalPage = parseInt(num / pageSize) + 1;
                } else {
                    totalPage = parseInt(num / pageSize);
                }
                currentPage = pno; //当前页数
                currentPage_ = currentPage;
                var tempStr = "共" + num + "条记录 当前第 " + currentPage +" / "+ totalPage+ " 页";
                document.getElementById("pagesDom1").innerHTML = tempStr;
                if (currentPage > 1) {
                    $("#firstPage").removeClass("ban");
                    $("#prePage").removeClass("ban");
                } else {
                    $("#firstPage").addClass("ban");
                    $("#prePage").addClass("ban");
                }
                if (currentPage < totalPage) {
                    $("#nextPage").removeClass("ban")
                    $("#lastPage").removeClass("ban")
                } else {
                    $("#nextPage").addClass("ban");
                    $("#lastPage").addClass("ban");
                }
                $("#jumpWhere").val(currentPage);
                fns(currentPage)
                return currentPage;
            }
            function firstPage() {
                goPage(totals, 1);
            }
            function prePage() {
                if (currentPage > 1) {
                    goPage(totals, currentPage - 1);
                }
            }
            function nextPage() {
                if (currentPage < totalPage) {
                    goPage(totals, currentPage + 1);
                }
            }
            function lastPage() {
                goPage(totals, totalPage);
            }
            function jumpPage() {
                var num = parseInt($("#jumpWhere").val());
                if (num != currentPage_ && 1 < num && num <= totalPage) {
                    goPage(totals, num);
                }
            }
        </script>
    </body>
</html>

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZhenGangLiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值