jquery实现分页,自己编写的jquery分页插件,通用性很强

在这里插入图片描述
html代码

<div id="page" class="paging"></div>

调用
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="./page.js"></script>
<script type="text/javascript">
	$("#page").paging({
		total: 100,
		numberPage: 10
	},
	function(msg) {
		//回调函数 msg为选中页码,可以发送ajax
		console.log(msg)
		}
	);
</script>

js代码

(function($) {
	var Paging = function(ele, options, call) {
		that = this;
		this.ele = ele;
		this.defaults = {
			numberPage: 5
		};
		this.call = call;
		this.options = $.extend({}, this.defaults, options);
		this.activePage = 1;
	};
	Paging.prototype = {
		init: function() { 
			if (this.options.total > 0) {
				this.pageNumber = Math.ceil(this.options.total / this.options.numberPage);
				this.initButton();
			}
			return this.ele; 
		},
		initButton: function() {
			this.ele.empty().append("<button class='prePage'>上一页</button>");
			for (var i = 0; i < this.pageNumber; i++) {
				var num = parseInt(i + 1);
				if (this.pageNumber > 6) {
					if (num > this.pageNumber - 3) {
						this.ele.append("<button class='pageButton' value='" + num + "'>" + num +
							"</button>");
					}
					if (num < 4) {
						this.ele.append("<button class='pageButton' value='" + num + "'>" + num +
							"</button>");
						if (num == 3) {
							this.ele.append("<span>...</span>")
						}
					}
				} else {
					this.ele.append("<button class='pageButton' value='" + num + "'>" + num + "</button>");
				}
				if (i == this.pageNumber - 1) {
					this.ele.append("<button class='nextPage'>下一页</button>");
					this.ele.find("[value=1]").addClass("activity");
					this.events()
				}
			}
		},
		events: function() {
			var that = this;
			this.ele.find(".pageButton").on("click", function() {
				that.activePage = $(this).val();
				that.call(that.activePage);
				that.styles($(this).val())
			});
			this.ele.find(".nextPage").on("click", function() {
				if (that.activePage < that.pageNumber) {
					that.activePage = parseInt(that.activePage) + 1;
					that.call(that.activePage);
					that.styles(that.activePage)
				}
			});
			this.ele.find(".prePage").on("click", function() {
				if (that.activePage > 1) {
					that.activePage -= 1;
					that.call(that.activePage);
					that.styles(that.activePage)
				}
			})
		},
		styles: function(val) {
			if (val > 2 && val < this.pageNumber - 1 && this.pageNumber > 6) {
				this.ele.empty().append("<button class='prePage'>上一页</button>");
				if (val > 3) {
					this.ele.append("<button class='pageButton' value='1'>1</button>");
				}
				if (val > 4) {
					this.ele.append("<span>...</span>")
				}
				this.ele.append("<button class='pageButton' value='" + parseInt(val - 2) + "'>" + parseInt(
					val - 2) + "</button>");
				this.ele.append("<button class='pageButton' value='" + parseInt(val - 1) + "'>" + parseInt(
					val - 1) + "</button>");
				this.ele.append("<button class='pageButton' value='" + parseInt(val) + "'>" + parseInt(
					val) + "</button>");
				var num = parseInt(val) + 1;
				this.ele.append("<button class='pageButton' value='" + num + "'>" + num + "</button>");
				var num = parseInt(val) + 2;
				this.ele.append("<button class='pageButton' value='" + num + "'>" + num + "</button>");
				if (val < this.pageNumber - 3) {
					this.ele.append("<span>...</span>")
				}
				if (val < this.pageNumber - 2) {
					this.ele.append("<button class='pageButton' value='" + this.pageNumber + "'>" + this
						.pageNumber + "</button>")
				}
				this.ele.append("<button class='nextPage'>下一页</button>");
				//that.events()
			}
			if (val <= 2 && this.pageNumber > 6) {
				this.ele.empty().append("<button class='prePage'>上一页</button>");
				this.ele.append("<button class='pageButton' value='1'>1</button>");
				this.ele.append("<button class='pageButton' value='2'>2</button>");
				this.ele.append("<button class='pageButton' value='3'>3</button>");
				this.ele.append("<span>...</span>");
				var num = parseInt(this.pageNumber) - 2;
				this.ele.append("<button class='pageButton' value='" + num + "'>" + num + "</button>");
				var num = parseInt(this.pageNumber) - 1;
				this.ele.append("<button class='pageButton' value='" + num + "'>" + num + "</button>");
				this.ele.append("<button class='pageButton' value='" + this.pageNumber + "'>" + this
					.pageNumber + "</button>");
				this.ele.append("<button class='nextPage'>下一页</button>");
			}
			if (val >= this.pageNumber - 1 && this.pageNumber > 6) {
				this.ele.empty().append("<button class='prePage'>上一页</button>");
				this.ele.append("<button class='pageButton' value='1'>1</button>");
				this.ele.append("<button class='pageButton' value='2'>2</button>");
				this.ele.append("<button class='pageButton' value='3'>3</button>");
				this.ele.append("<span>...</span>");
				var num = parseInt(this.pageNumber) - 2;
				this.ele.append("<button class='pageButton' value='" + num + "'>" + num + "</button>");
				var num = parseInt(this.pageNumber) - 1;
				this.ele.append("<button class='pageButton' value='" + num + "'>" + num + "</button>");
				this.ele.append("<button class='pageButton' value='" + this.pageNumber + "'>" + this
					.pageNumber + "</button>");
				this.ele.append("<button class='nextPage'>下一页</button>");
			}
			if (this.pageNumber > 6) {
				this.events();
			}
			this.ele.find(".pageButton").removeClass("activity");
			this.ele.find("[value=" + val + "]").addClass("activity");
		}
	};
	$.fn.paging = function(options, call) {
		var paging = new Paging(this, options, call);
		return paging.init();
	};
})(jQuery);

css代码

<style type="text/css">
	.paging button {
		margin: 5px;
		background: #237eff;
		color: #fff;
		padding: 5px 10px;
		border: none;
		cursor: pointer;
	}
	.paging .activity {
		color: #ffffff;
		background: #cccccc;
	}
</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值