首页 > 代码库 > jQuery----分页插件实现
jQuery----分页插件实现
1、效果
2、html代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jquery 分页插件</title> <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/css/pagination.less"> </head> <body> <div class="pagination-container" id = ‘pagination-container‘> </div> <script type="text/javascript" src=http://www.mamicode.com/‘js/jquery-2.2.1.min.js‘></script>"text/javascript" src=http://www.mamicode.com/‘js/pagination.js‘></script>>
3、less代码
.pagination-container{ width: 800px; height: 80px; margin: 0 auto; text-align: center; .div{ height: 40px; line-height: 30px; margin: 0 auto; div{ display: inline-block; } input,span,a{ margin: 0 10px 0 0; } input{ width: 40px; height: 20px; } .search-page-btn{ height: 20px; padding: 4px 10px; background-color: #568DBD; color: white; border: 1px solid #568DBD; &:hover{ background-color: #4681AF; } } a{ padding: 6px 10px; border: 1px solid #C5B7B7; text-decoration: none; color: #888888; font-size: 12px; &:hover{ border: 1px solid #568DBD; } } .choose{ background-color: #568DBD; color: white; font-weight: bold; } } }
4、jquery代码
var pagination = $("#pagination-container"); var now_page = 1; var total_page = 30; createPagination(pagination, now_page, total_page); function createPagination(pagination, now_page, total_page) { initPagination(); addPaginationEvent(); function initPagination() { pagination.empty(); var lineOne = $("<div class = ‘div‘>" + "<span>当前页:</span>" + "<span class=‘now-page num‘>" + now_page + "</span>" + "<span>/</span>" + "<span class=‘num total-page‘>" + total_page + "</span>" + "<span>跳转到</span>" + "<input type=‘number‘ name=‘number‘ class=‘jump-top-page‘ value = http://www.mamicode.com/‘" + now_page + "‘ tabindex = ‘1‘>" + "<a href=http://www.mamicode.com/‘#‘ class=‘search-page-btn‘>确定" + "</div>"); pagination.append(lineOne); var pageObj = $("<div class = ‘div‘>" + "<div class = ‘left‘>" + "<a href=http://www.mamicode.com/‘#‘ class=‘first-page‘>首页" + "<a href=http://www.mamicode.com/‘#‘ class=‘prev-page‘><上一页" + "</div>" + "<div class = ‘center‘>" + "</div>" + "<div class = ‘right‘>" + "<a href=http://www.mamicode.com/‘#‘ class=‘next-page‘>下一页>" + "<a href=http://www.mamicode.com/‘#‘ class=‘last-page‘>尾页" + "</div>" + "</div>"); pagination.append(pageObj); fillNumberPage(1); } function addPaginationEvent() { var searchBtn = pagination.find(".search-page-btn"); var firstPageBtn = pagination.find(".first-page"); var lastPageBtn = pagination.find(".last-page"); var prevPageBtn = pagination.find(".prev-page"); var nextPageBtn = pagination.find(".next-page"); var prevPagesBtn = pagination.find(".prev-pages"); var nextPagesBtn = pagination.find(".next-pages"); var searchVal = pagination.find(".jump-top-page"); searchBtn.on("click", function() { var searchVal = pagination.find(".jump-top-page"); var page = parseInt(searchVal.val()); if (page <= total_page && page > 0) { now_page = page; fillNumberPage(page); } }) firstPageBtn.on("click", function() { if (now_page != 1) { fillNumberPage(1); now_page = 1; } }) lastPageBtn.on("click", function() { if (now_page != total_page) { now_page = total_page; fillNumberPage(total_page); } }) prevPageBtn.on("click", function() { if (now_page > 1) { now_page--; fillNumberPage(now_page); } }) nextPageBtn.on("click", function() { if (now_page < total_page) { now_page++; fillNumberPage(now_page); } }) pagination.on("click", ".page", function() { var page = parseInt($(this).attr("data-page")); now_page = page; fillNumberPage(page); }) $(document).on("keydown",function(event){ if(event.keyCode == ‘13‘){ searchBtn.click(); } }) } function fillNumberPage(page) { pagination.find(".center").empty(); pagination.find(".now-page").text(page); pagination.find(".jump-top-page").val(page); var start; var end; var p = page; if (total_page <= 7) { start = 1; end = total_page; } else { if (page <= 4) { start = 1; end = 7; } else if (page > 4 && page < total_page - 4) { start = page - 3; end = page + 3; } else { start = total_page - 7; end = total_page; } } for (var i = start; i <= end; i++) { var pageCenter = $("<a href=http://www.mamicode.com/‘#‘ class=‘page‘ data-page = ‘" + i + "‘>" + i + "</a>"); if (i == p) { pageCenter.addClass("choose"); } pagination.find(".center").append(pageCenter); } } }
5、总结
将父节点,当前页,总页数传给创建函数就可以生成分页。
(还没加跳页的功能)
jQuery----分页插件实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。