Pure jquery implements simple paging effect (code example)

青灯夜游
Release: 2018-10-20 15:26:48
Original
4864 people have browsed it

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; }
Copy after login

html code:

最简单的分页

    Copy after login

    js script file

    1. Reference Jquery and paging script

     
    Copy after login

    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 ="
  • 上一页
  • "; $("#currentPage").val(curPage); if(page <= totalPage){ if(datas <= 10){ for(i=1; i<=datas; i++){ //为当前页增加样式 var active =""; if(i==curPage){ active=" class='active' "; } liStr +="
  • "+i+"
  • " } }else{ var start =pageSize*(page-1)+1; var end =page*pageSize; if(page == totalPage){ for(i=start; i<=datas; i++){ //为当前页增加样式 var active =""; if(i==curPage){ active=" class='active' "; } liStr +="
  • "+i+"
  • " } }else{ for(i=start; i<=end; i++){ //为当前页增加样式 var active =""; if(i==curPage){ active=" class='active' "; } liStr +="
  • "+i+"
  • " } } } } liStr +="
  • 下一页
  • "; $(".pagination").append(liStr); } })
    Copy after login

    Let’s take a look at the rendering:

    Pure jquery implements simple paging effect (code example)

    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!

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!