美文网首页
封装基于JQ翻页插件

封装基于JQ翻页插件

作者: Vivian_06e6 | 来源:发表于2018-10-13 16:41 被阅读0次

js

(function ($) {
    function init(dom, args) {
        if (args.current <= args.pageCount) {
            fillHtml(dom, args);
            bindEvent(dom, args);
        } else {
            alert('请输入正确页数')
        }
    };
    
    function fillHtml(dom, args) {
        dom.empty();
        //上一页
        if (args.current > 1) {
            dom.append('<a href = "#" class="prevPage">上一页</a>');
        } else {
            dom.remove('.prevPage');
            dom.append('<span class="disabled">上一页</span>');
        }
        //中间页数
        if (args.current != 1 && args.current >= 4 && args.pageCount != 4) {
            dom.append('<a href = "#" class="tcdNumber">' + 1 + '</a>');
        }
        if (args.current - 2 > 2 && args.current <= args.pageCount && args.pageCount > 5) {
            dom.append('<span>...</span>');
        }
        var start = args.current - 2;
        var end = args.current + 2;
        for (; start <= end; start++) {
            if (start <= args.pageCount && start >= 1) {
                if (start != args.current) {
                    dom.append('<a href = "#" class="tcdNumber">' + start + '</a>');
                } else {
                    dom.append('<span class="current">' + start + '</span>');
                }
            }
        }

         
        if (args.current + 2 < args.pageCount - 1 && args.pageCount > 5) {
            dom.append('<span>...</span>')
        }

        if (args.current != args.pageCount && args.current < args.pageCount - 2 && args.pageCount != 4) {
            dom.append('<a href="#" class="tcdNumber">' + args.pageCount + '</a>');
        }
        //下一页
        if (args.current < args.pageCount) {
            dom.append('<a href = "#" class="nextPage">下一页</a>');
        } else {
            dom.remove('.nextPage');
            dom.append('<span class="disabled">下一页</span>');
        }
    };

    function bindEvent(obj, args) {
        //点击页码
        obj.on('click', '.tcdNumber', function () {
            var current = parseInt($(this).text());
            changePage(obj,args,current);
        })
        //上一页
        // a.prevPage   规定只能添加到指定的子元素上的事件处理程序
        obj.on('click', '.prevPage', function () {
            var current = parseInt(obj.children('.current').text());
            changePage(obj,args,current-1);            
        })
        //下一页
        obj.on('click', '.nextPage', function () {
            var current = parseInt(obj.children('.current').text());
            changePage(obj,args,current + 1);
        })
    };

    function changePage(dom, args,page) {
        fillHtml(dom, { 'current': page, 'pageCount': args.pageCount });
        if (typeof (args.backFn == "function")) {
            args.backFn(page);
        }
    }

    $.fn.createPage = function (options) {
        var args = $.extend({
            pageCount: 5,
            current: 2,
            backFn: function () { }
        }, options);
        init(this, args)
    }
})(jQuery);

css

*{
    margin:0;
    padding: 0;
    list-style: none;
    text-decoration: none;
}
.page{
    width:500px;
    margin:100px auto;
    color: #ccc;
}
.page a{
    display: inline-block;
    color: #428bca;
    height: 25px;
    line-height: 25px;
    padding: 0 10px;
    border: 1px solid #ddd;
    margin: 0 2px;
    border-radius: 4px;
    vertical-align: middle;
}
.page a:hover{
    border: 1px solid #428bca;
}
.page .current{
    display: inline-block;
    height: 25px;
    line-height: 25px;
    padding: 0 10px;
    margin: 0 2px;
    color: #fff;
    background-color: #428bca;
    border: 1px solid #428bca;
    border-radius: 4px;
    vertical-align: middle;
}
.page .disabled{
    display: inline-block;
    height: 25px;
    line-height: 25px;
    padding: 0 10px;
    margin: 0 2px;
    color: #bfbfbf;
    background: #f2f2f2;
    border: 1px solid #bfbfbf;
    border-radius: 4px;
    vertical-align: middle;
}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>翻页</title>
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <div class="page"></div>
    <script src="jquery.js"></script>
    <script src="index1.js"></script>
    
    <script>
        $('.page').createPage({
            pageCount:13,
            current:3,
            backFn:function(p){
                console.log(p)
            }
        })
    </script>
</body>
</html>

相关文章

  • 封装基于JQ翻页插件

    js css html

  • 封装jq插件

    参考:http://blog.csdn.net/qq_19244423/article/details/46813337

  • jq封装插件

    读代码-梳理步骤(文字记录)-实现代码-学会调试http://hemin.cn/jq/ jq工具http://...

  • jq封装插件

    1.插件参数 插件用于减少代码量,防止无谓的粘贴复制,所以,要尽可能的强大,具有包容性,呵呵参数设置:默认的,可修...

  • 基于jq封装ajax请求

    工作中常用到ajax请求,自己又不想重复写这些代码,于是自己封装了一个请求。 如何调用:

  • hz-mongoose快速入门

    //what://mongoose是mongodb的一种封装。类似js和jq的关系。//mongoose是一个插件...

  • 简单实用的jQuery分页插件

    在做商城和订单管理的时候,常常会用到分页功能,所以我封装了一个jQuery的分页插件,该插件主要实现上下翻页,输入...

  • h5支付键盘

    payBoard payBoard 是一个移动端ui插件,实现了支付时调用h5键盘的功能。 本插件基于jq开发,使...

  • 基于原生JS封装的Modal对话框插件

    基于原生JS封装Modal对话框插件 原生JS封装Modal对话框插件,个人用来学习原理与思想,只有简单的基本框架...

  • 基于jQ的特效插件:SuperSlide

    官网:http://www.superslide2.com/index.htmlGitHub:https://gi...

网友评论

      本文标题:封装基于JQ翻页插件

      本文链接:https://www.haomeiwen.com/subject/argunftx.html