Pagination using JQuery
If you have a large number of pages, then use pagination to each page. This tutorial helps you when you want to produce paging on page.
Use this code for pagination using JQuery :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pagination using JQuery</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<style>
.active {
color: green;
}
#pages {
clear: both;
padding: 10px;
width: 122px;
margin: auto;
list-style: none;
}
#pages li {
float:left;
margin-right:10px;
}
#pages li a {
display:block;
color:#717171;
text-shadow:0px 1px white;
padding:8px 16px;
-webkit-border-radius:3px;
-moz-border-radius:3px;
border-radius:50%;
-webkit-box-shadow:0px 1px 3px 0px rgba(0,0,0,0.35);
-moz-box-shadow:0px 1px 3px 0px rgba(0,0,0,0.35);
box-shadow:0px 1px 3px 0px rgba(0,0,0,0.35);
background:#f9f9f9;
background:-webkit-linear-gradient(top,#f9f9f9 0%,#e8e8e8 100%);
background:-moz-linear-gradient(top,#f9f9f9 0%,#e8e8e8 100%);
background:-o-linear-gradient(top,#f9f9f9 0%,#e8e8e8 100%);
background:-ms-linear-gradient(top,#f9f9f9 0%,#e8e8e8 100%);
}
#pages li a.active {
color: white;
text-shadow: 0px 1px #777;
-webkit-box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.8);
-moz-box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.8);
box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.8);
background: #333;
background: -webkit-linear-gradient(top,#ccc 0%,#999 100%);
}
#pages li a:hover {
-webkit-box-shadow:0px 1px 3px 0px rgba(0,0,0,0.55);
-moz-box-shadow:0px 1px 3px 0px rgba(0,0,0,0.55);
box-shadow:0px 1px 3px 0px rgba(0,0,0,0.55);
background:#fff;
background:-webkit-linear-gradient(top,#fff 0%,#e8e8e8 100%);
background:-moz-linear-gradient(top,#fff 0%,#e8e8e8 100%);
background:-o-linear-gradient(top,#fff 0%,#e8e8e8 100%);
background:-ms-linear-gradient(top,#fff 0%,#e8e8e8 100%);
}
#pages li a:active,#pages li a.active:active {
-webkit-box-shadow:inset 0px 1px 3px 0px rgba(0,0,0,0.5),0px 1px 1px 0px rgba(255,255,255,1) !important;
-moz-box-shadow:inset 0px 1px 3px 0px rgba(0,0,0,0.5),0px 1px 1px 0px rgba(255,255,255,1) !important;
box-shadow:inset 0px 1px 3px 0px rgba(0,0,0,0.5),0px 1px 1px 0px rgba(255,255,255,1) !important;
}
#pages li a.active:hover {
-webkit-box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.9);
-moz-box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.9);
box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.9);
background: #333;
background: -webkit-linear-gradient(top,#ccc 0%,#777 100%);
}
.block{
height: 200px;
background: lightgray;
padding: 20px;
}
</style>
<body>
<section>
<!-- Main Container -->
<div class="container">
<!-- Content Box -->
<!-- Row -->
<div class="row">
<!-- Heading Area -->
<div class="col-md-12 col-xs-12 col-sm-12" >
<div class="row">
<div class="col-xs-12 col-md-12 col-sm-12">
<h3 class="text-center">
Pagination using JQuery
</h3>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 content">
<div class="block">
<h1 class="text-center">1 Div</h1>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 content">
<div class="block">
<h1 class="text-center">2 Div</h1>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 content">
<div class="block">
<h1 class="text-center">3 Div</h1>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 content">
<div class="block">
<h1 class="text-center">4 Div</h1>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 content">
<div class="block">
<h1 class="text-center">5 Div</h1>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 content">
<div class="block">
<h1 class="text-center">6 Div</h1>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 content">
<div class="block">
<h1 class="text-center">7 Div</h1>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 content">
<div class="block">
<h1 class="text-center">8 Div</h1>
</div>
</div>
<div class="col-xs-12 col-md-12 col-sm-12">
<ul id="pages">
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<!-- Row End -->
</div>
<!-- Main Container End -->
</section>
<script>
pageSize = 4;
var pageCount = $(".content").length / pageSize;
for(var i = 0 ; i<pageCount;i++){
$("#pages").append('<li><a href="#">'+(i+1)+'</a></li> ');
}
$("#pages li").first().find("a").addClass("active")
showPage = function(page) {
$(".content").hide();
$(".content").each(function(n) {
if (n >= pageSize * (page - 1) && n < pageSize * page)
$(this).show();
});
}
showPage(1);
$("#pages li a").click(function() {
$("#pages li a").removeClass("active");
$(this).addClass("active");
showPage(parseInt($(this).text()))
});
</script>
</body>
</html>
Thank you for sharing this useful information. it is very helpful.
ReplyDeleteBest Web Development Services India