How to achieve paging effect with pure jquery? What this article brings to you is pure jquery to achieve simple paging effect (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
During our front-end page development process, if a page displays too much data and the page is too long, the user experience will be degraded. At this time, we need to divide the data into several pages for display, which is called paging.
Below we will use a simple code example to introduce in detail how to usepure jquery to achieve a front-end super simple paging effect!
css code:
ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; border: 1px solid blue; border-radius:3px; } .active{ background-color: #7FFFAA; }
html code:
最简单的分页
js script file
1. Reference Jquery and paging script
2. Write jquery.PageBar.js script
$().ready(function(){ var curPage =$("#currentPage").val(); var last =$("#totalPage").val(); var page =Math.ceil(curPage/10); //调用绘制分页样式函数 draw(page,curPage); //绑定点击页码事件 $(document).on("click",".pagination li a",function(){ var str =$(this).html(); if(!isNaN(str)){ //移除之前的active $(".pagination li a").removeClass("active"); $(this).attr("class","active"); $("#currentPage").val(str); } }); //绑定下一页点击事件 $(document).on("click","#nextPage",function(){ var num =$(".active").html(); var curPage =$("#currentPage").val(); var last =$("#totalPage").val(); var page =Math.ceil((parseInt(num))/10); if(num < page*10 && num != last){ //移除之前的active $(".pagination li a").removeClass("active"); $("#"+(parseInt(num)+1)+"").attr("class","active"); //$(".pagination li a[text="+(parseInt(num)+1)+"]").attr("class","active");//jQ1.6支持 $("#currentPage").val(parseInt(num)+1); }else if(num == page*10 && num != last){ //清空之前的数据 $(".pagination").html(""); draw(page+1,(parseInt(num)+1)); } }) //绑定上一页点击事件 $(document).on("click","#previousPage",function(){ var num =$(".active").html(); var curPage =$("#currentPage").val(); var last =$("#totalPage").val(); var page =Math.ceil((parseInt(num))/10); if(num <= page*10 && num != (page-1)*10+1){ //移除之前的active $(".pagination li a").removeClass("active"); $("#"+(parseInt(num)-1)+"").attr("class","active"); //$(".pagination li a[text="+(parseInt(num)+1)+"]").attr("class","active");//jQ1.6支持 $("#currentPage").val(parseInt(num)-1); }else if(num == (page-1)*10+1 && num != 1){ //清空之前的数据 $(".pagination").html(""); draw(page-1,(parseInt(num)-1)); } }) //绘制页面分页样式 function draw(page,curPage){ //页面中的当前页 var page =page; //后台传过来的页数 var curPage = curPage; //后台传过来的总页数 var datas =$("#totalPage").val(); //每页显示多少条数据 var pageSize =10; //在网页中一共要分多少页 var totalPage = Math.floor((datas-1)/pageSize+1); var liStr ="
Let’s take a look at the rendering:
The above picture is just a static picture, no The method shows the effect, and you can try it yourself. Hope it helps those in need!
If you want to learn more about front-end related knowledge and get more related tutorials, you can visit:jQuery video tutorial,jquery special effects collection,bootstrap video tutorial!
The above is the detailed content of Pure jquery implements simple paging effect (code example). For more information, please follow other related articles on the PHP Chinese website!