window.history.pushState & Pagination
by destrada
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="stylesheet" href="https://bootswatch.com/superhero/bootstrap.css">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<title>jQuery Pagination Tutorial</title>
</head>
<body>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
</div>
</nav>
<div class="container">
<nav aria-label=...>
<ul class=pagination>
<li id="previous-page">
<a href="javascript:void(0)" aria-label=Previous>
<span aria-hidden=true>PREV</span>
</a>
</li>
</ul>
</nav>
<div id="page">
<div class="list-group">
<a href="javascript:void(0)" class="list-group-item active">
<h4 class="list-group-item-heading">Title 1</h4>
<p class="list-group-item-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum...
CSS
body {
padding-top: 50px;
background-color:red;
}
.starter-template {
padding: 40px 15px;
text-align: center;
}
JavaScript
'use strict';
var numberOfItems = $('#page .list-group').length; // Get total number of the items that should be paginated
var limitPerPage = 4; // Limit of items per each page
$('#page .list-group:gt(' + (limitPerPage - 1) + ')').hide(); // Hide all items over page limits (e.g., 5th item, 6th item, etc.)
var totalPages = Math.round(numberOfItems / limitPerPage); // Get number of pages
$(".pagination").append("<li class='current-page active'><a href='javascript:void(0)'>" + 1 + "</a></li>"); // Add first page marker
// Loop to insert page number for each sets of items equal to page limit (e.g., limit of 4 with 20 total items = insert 5 pages)
for (var i = 2; i <= totalPages; i++) {
$(".pagination").append("<li class='current-page'><a href='javascript:void(0)'>" + i + "</a></li>"); // Insert page number into pagination tabs
}
// Add next button after all the page numbers
$(".pagination").append("<li id='next-page'><a href='javascript:void(0)' aria-label=Next><span aria-hidden=true>NEXT</span></a></li>");
// Function that displays new items based on page number that was clicked
$(".pagination li.current-page").on("click", function() {
// Check if page number that was clicked on is the current page that is being displayed
if ($(this).hasClass('active')) {
return false; // Return false (i.e., nothing to do, since user clicked on the page number that is already being displayed)
} else {
var currentPage = $(this).index(); // Get the current page number
$(".pagination li").removeClass('active'); // Remove the 'active' class status from the page that is currently being displayed
$(this).addClass('active'); // Add the 'active' class status to the page that was clicked on
$("#page .list-group").hide(); // Hide all items in loop, this case, all the list groups
var grandTotal = limitPerPage * currentPage; // Get the total number of items up to the page number that was clicked on
// Loop through total items, selecting a new set of...