Home >Web Front-end >JS Tutorial >Pure jquery implements simple paging effect (code example)

Pure jquery implements simple paging effect (code example)

青灯夜游
青灯夜游Original
2018-10-20 15:26:484988browse

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 use pure 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:

<h2>最简单的分页</h2>
<ul class="pagination"></ul>
<input type="hidden" id="currentPage" name="currentPage" value="1"></input>
<input type="hidden" id="totalPage" value="54"></input>

js script file

1. Reference Jquery and paging script

<script src="/js/jquery.min.js" type="text/javascript"></script>
<script src="/js/jquery.PageBar.js" type="text/javascript"></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 ="<li><a id=&#39;previousPage&#39; href=&#39;#&#39;>上一页</a></li>";
		$("#currentPage").val(curPage);
		if(page <= totalPage){
			if(datas <= 10){
				for(i=1; i<=datas; i++){
					//为当前页增加样式
					var active ="";
					if(i==curPage){
						active=" class=&#39;active&#39; ";
					}
					liStr +=" <li><a id="+i+" "+active+" href=&#39;#&#39;>"+i+"</a></li>"
				}
			}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=&#39;active&#39; ";
						}
						liStr +=" <li><a id="+i+" "+active+" href=&#39;#&#39;>"+i+"</a></li>"
					}
				}else{
					for(i=start; i<=end; i++){
						//为当前页增加样式
						var active ="";
						if(i==curPage){
							active=" class=&#39;active&#39; ";
						}
						liStr +=" <li><a id="+i+" "+active+" href=&#39;#&#39;>"+i+"</a></li>"
					}
				}
			}
		}
		liStr +="<li><a id=&#39;nextPage&#39; href=&#39;#&#39;>下一页</a></li>";
		$(".pagination").append(liStr);
	}
})

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!

Statement:
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