JavaScript Pagination: Simplifying Web Content Navigation

Faraz

By Faraz -

Learn how to implement JavaScript pagination for efficient content navigation on your web applications. Step-by-step guide with code examples.


JavaScript Pagination Simplifying Web Content Navigation.jpg

Table of Contents

  1. Project Introduction
  2. HTML Code
  3. CSS Code
  4. JavaScript Code
  5. Preview
  6. Conclusion

In the world of web development, handling large amounts of content or displaying multiple pages of information can be a challenge. This is where JavaScript pagination comes to the rescue. Pagination allows you to divide your content into smaller, more manageable chunks, making it easier for users to navigate through and explore.

JavaScript pagination involves breaking down your content into logical pages and presenting them to users in a controlled manner. Instead of overwhelming them with massive data, you can display a subset of results per page, providing a more user-friendly experience.

Implementing pagination in your web applications can enhance user experience, improve performance, and optimize resource utilization. Users can easily navigate through different pages, find the information they need, and have a smoother interaction with your website or application.

Throughout this blog post, we will guide you through the process of implementing JavaScript pagination. We'll cover the basic concepts, step-by-step instructions, advanced techniques, and best practices. Whether you're a beginner or an experienced developer, this article will equip you with the knowledge and skills to effectively implement pagination in your JavaScript-powered projects.

So, let's embark on this journey to simplify web content navigation with JavaScript pagination. By the end of this article, you'll have the tools to create a seamless and engaging user experience for your web application's content. Let's get started!

Join My Telegram Channel to Download the Projects Source Code: Click Here

Prerequisites:

Before starting this tutorial, you should have a basic understanding of HTML, CSS, and JavaScript. Additionally, you will need a code editor such as Visual Studio Code or Sublime Text to write and save your code.

Source Code

Step 1 (HTML Code):

To get started, we will first need to create a basic HTML file. In this file, we will include the main structure for our pagination.

After creating the files just paste the following codes into your file. Make sure to save your HTML document with a .html extension, so that it can be properly viewed in a web browser.

Let's break it down:

1. <!DOCTYPE html>: This declaration informs the web browser that the document is written in HTML5.

2. <html lang="en">: This tag denotes the opening of the HTML document and specifies the language as English ("en").

3. <head>: This section contains meta-information about the HTML document, such as the title, character encoding, and stylesheets.

4. <title>JavaScript Pagination</title>: This tag sets the title of the web page, which will be displayed in the browser's title bar or tab.

5. <meta charset="UTF-8" />: This meta tag defines the character encoding for the web page as UTF-8, which supports a wide range of characters.

6. <meta name="viewport" content="width=device-width" />: This meta tag specifies how the web page should be displayed on different devices by setting the width of the viewport to match the device's width.

7. <link rel="stylesheet" href="styles.css" />: This link tag references an external CSS file called "styles.css" and includes it in the HTML document. The CSS file is used to define the styles and layout of the web page.

8. <body>: This section represents the main content of the web page.

9. <div id="pagination"></div>: This div element with the id "pagination" serves as a placeholder or container where the JavaScript code will generate and display pagination elements.

10. <script src="script.js"></script>: This script tag references an external JavaScript file called "script.js" and includes it in the HTML document. The JavaScript code within this file will be executed, allowing dynamic functionality and interactivity on the web page.

11. </body>: This tag denotes the closing of the body section.

12. </html>: This tag denotes the closing of the HTML document.

This is the basic structure of our pagination using HTML, and now we can move on to styling it using CSS.

Step 2 (CSS Code):

Once the basic HTML structure of the pagination is in place, the next step is to add styling to the pagination using CSS.

Next, we will create our CSS file. In this file, we will use some basic CSS rules to create our pagination.

Here's a breakdown of what each section does:

1. Styling for the <html> element:

  • height: 100% and width: 100% set the height and width of the <html> element to occupy the entire viewport.
  • background-color: #D7D7D7 sets the background color of the <html> element to a light gray (#D7D7D7).
  • The following lines set the background image of the <html> element to a radial gradient from #F2F2F2 to #D1D1D1. Multiple vendor-specific prefixes (-webkit-, -moz-, -ms-, -o-) are used to ensure cross-browser compatibility.

2. Styling for the <body> element:

  • margin: 0 removes any default margin around the <body> element.
  • height: 100% and width: 100% set the height and width of the <body> element to occupy the entire viewport.
  • text-align: center centers the content horizontally within the <body> element.
  • font-family: Arial, sans-serif sets the font family for the text content inside the <body> element to Arial or a generic sans-serif font.
  • background-image: url(data:image/png;base64,...) sets the background image of the <body> element to a base64-encoded PNG image. The long data URL represents the image data.

3. Styling for the body:before pseudo-element:

  • content: '' adds an empty content before the content of the <body> element.
  • display: inline-block makes the pseudo-element behave like an inline-block element.
  • width: 0 and height: 100% make the pseudo-element have no width and the same height as the <body> element.
  • vertical-align: middle vertically aligns the pseudo-element in the middle of its containing element.

4. Styling for the element with the ID "pagination":

  • display: inline-block makes the element behave like an inline-block element.
  • vertical-align: middle vertically aligns the element in the middle of its containing element.
  • border-radius: 4px sets the border radius to create rounded corners.
  • padding: 1px 2px 4px 2px sets the padding around the content of the element.
  • border-top: 1px solid #AEAEAE and border-bottom: 1px solid #FFFFFF define the top and bottom borders of the element.
  • The following lines set the background of the element to a linear gradient from #DBDBDB to #E2E2E2. Multiple vendor-specific prefixes are used for cross-browser compatibility.

5. Styling for the <a> and <i> elements within the element with the ID "pagination":

  • display: inline-block makes the elements behave like inline-block elements.
  • vertical-align: middle vertically aligns the elements in the middle of their containing element.
  • width: 22px sets the width of the elements to 22 pixels.
  • color: #7D7D7D sets the text color of the elements to a gray shade (#7D7D7D).
  • text-align: center horizontally centers the text within the elements.
  • font-size: 10px sets the font size of the elements to 10 pixels.
  • padding: 3px 0 2px 0 sets the padding around the content of the elements.
  • The lines prefixed with vendor-specific prefixes and the final line set the user-select property to none, disabling text selection for the elements.

6. Further styling for the <a> elements within the element with the ID "pagination":

  • margin: 0 2px 0 2px sets the margin around the elements.
  • border-radius: 4px sets the border radius to create rounded corners.
  • border: 1px solid #E3E3E3 sets a border for the elements.
  • cursor: pointer changes the cursor to a pointer when hovering over the elements.
  • box-shadow and text-shadow properties add shadow effects to the elements.
  • The following lines set the background of the elements to a linear gradient from #F3F3F3 to #D7D7D7. Multiple vendor-specific prefixes are used for cross-browser compatibility.

7. Styling for the <i> elements within the element with the ID "pagination":

  • margin: 0 3px 0 3px sets the margin around the elements.

8. Styling for the <a> elements with the class "current" within the element with the ID "pagination":

  • border: 1px solid #E9E9E9 sets a border for the elements.
  • box-shadow property adds a shadow effect to the elements.
  • The following lines set the background of the elements to a linear gradient from #D0D0D0 to #EBEBEB. Multiple vendor-specific prefixes are used for cross-browser compatibility.

This will give our pagination an upgraded presentation. Create a CSS file with the name of styles.css and paste the given codes into your CSS file. Remember that you must create a file with the .css extension.

html {
  height: 100%;
  width: 100%;
  background-color: #D7D7D7;
  background-image: -webkit-radial-gradient(contain, #F2F2F2, #D1D1D1);
  background-image:    -moz-radial-gradient(contain, #F2F2F2, #D1D1D1);
  background-image:     -ms-radial-gradient(contain, #F2F2F2, #D1D1D1);
  background-image:      -o-radial-gradient(contain, #F2F2F2, #D1D1D1);
  background-image:         radial-gradient(contain, #F2F2F2, #D1D1D1);
}
body {
  margin: 0;
  height: 100%;
  width: 100%;
  text-align: center;
  font-family: Arial, sans-serif;
  background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAKlBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKE86IAAAADnRSTlMAAQIDBAUGBwgJCgsMDRK8QVgAAAukSURBVHja3Z3XYuM6DEQPKXZy/v937wMoZXPjJC6yLWUftsVWAUFgMChEyoLhQdgvSQIQQqEzhFBbAFELyKPYxfoxIqWy4DKyqyixXgFalkBQgaXYLQIEAYwheWCAFzi7ouQE1KYMEIVIdhVBt4cVZHqBjm9Atpt6OQLrp4boQgN7XrGU+arLAqgiUPDQRFxFYM9PV5hiGWTQIqeQUW8sGUl0AmL07nBiQVSYkoGsYS/eBgtoNFrAKQkUZM8k0OiQ1BVxqNi/BUIFQUML0lwaoWaXzRmIgGJhifagKogFIupzFQFU7I+hJNUpZ+jghFButvbkLEQQUetX/aCXWCSgr4qyILxpx6Y7QwCOOrDPerQQIMeFTEYUFLvJSMEHRGhjaouLQ9AGBBZ7MQmhBFkRaQjJBSRagQSFtH2wBEm19iiU0dRkEZ0t9bYyXah0aUAogEgSdIEJJ9vDQ9diYlIEuXnJplEIjIii2KRc6nZTxyoCwWLiDBBlywiIgOsSpZNUionRDwFdWVIaZEZztnJp1WAlLS6DxGCxNYpFwDI3MKZeFYRDgE8Ayd7di2Fr2wExQODwrDsaRRJTO9u8aESj0BhSJ87tXQckMkgdSAn7yhAoQyeMLDVYXIbaqA2/2gYViRJBFKVtGZvITvNlxtStSMYDqFOxl4OEcFORR0TURU1TbextqpkhMMlLGtiVbO/b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DhuVIYfdjOaBjkfD3Hwv9H6hF+t8WGk8EAAAAAElFTkSuQmCC);
}
body:before {
  content: '';
  display: inline-block;
  width: 0; height: 100%;
  vertical-align: middle;
}

#pagination {
  display: inline-block;
  vertical-align: middle;
  border-radius: 4px;
  padding: 1px 2px 4px 2px;
  border-top: 1px solid #AEAEAE;
  border-bottom: 1px solid #FFFFFF;
  background-color: #DADADA;
  background-image: -webkit-linear-gradient(top, #DBDBDB, #E2E2E2);
  background-image:    -moz-linear-gradient(top, #DBDBDB, #E2E2E2);
  background-image:     -ms-linear-gradient(top, #DBDBDB, #E2E2E2);
  background-image:      -o-linear-gradient(top, #DBDBDB, #E2E2E2);
  background-image:         linear-gradient(top, #DBDBDB, #E2E2E2);
}
#pagination a, #pagination i {
  display: inline-block;
  vertical-align: middle;
  width: 22px;
  color: #7D7D7D;
  text-align: center;
  font-size: 10px;
  padding: 3px 0 2px 0;
  -webkit-user-select:none;
     -moz-user-select:none;
      -ms-user-select:none;
       -o-user-select:none;
          user-select:none;
}

#pagination a {
  margin: 0 2px 0 2px;
  border-radius: 4px;
  border: 1px solid #E3E3E3;
  cursor: pointer;
  box-shadow: inset 0 1px 0 0 #FFF, 0 1px 2px #666;
  text-shadow: 0 1px 1px #FFF;
  background-color: #E6E6E6;
  background-image: -webkit-linear-gradient(top, #F3F3F3, #D7D7D7);
  background-image:    -moz-linear-gradient(top, #F3F3F3, #D7D7D7);
  background-image:     -ms-linear-gradient(top, #F3F3F3, #D7D7D7);
  background-image:      -o-linear-gradient(top, #F3F3F3, #D7D7D7);
  background-image:         linear-gradient(top, #F3F3F3, #D7D7D7);
}
#pagination i {
  margin: 0 3px 0 3px;
}
#pagination a.current {
  border: 1px solid #E9E9E9;
  box-shadow: 0 1px 1px #999;
  background-color: #DFDFDF;
  background-image: -webkit-linear-gradient(top, #D0D0D0, #EBEBEB);
  background-image:    -moz-linear-gradient(top, #D0D0D0, #EBEBEB);
  background-image:     -ms-linear-gradient(top, #D0D0D0, #EBEBEB);
  background-image:      -o-linear-gradient(top, #D0D0D0, #EBEBEB);
  background-image:         linear-gradient(top, #D0D0D0, #EBEBEB);
} 

Step 3 (JavaScript Code):

Finally, we need to create a function in JavaScript.

Let's go through the code and explain each part:

The code starts by defining an object called Pagination, which will hold all the pagination-related functionality.

The Extend method is used to initialize the pagination data. It takes an optional data object as a parameter, which can contain the following properties:

  • size: The total number of pages (default: 300).
  • page: The currently selected page (default: 1).
  • step: The number of pages to show before and after the current page (default: 3).

The Add method is responsible for generating the HTML code for the individual page numbers between a given range (from s to f). It appends the generated HTML code to the code property of the Pagination object.

The Last method adds the last page number along with an ellipsis separator to the code property.

The First method adds the first page number along with an ellipsis separator to the code property.

The Click method is called when a page number is clicked. It updates the Pagination.page value to the clicked page number and calls the Start method to regenerate the pagination.

The Prev method is called when the previous button is clicked. It decreases the Pagination.page value and ensures that it doesn't go below 1. Then it calls the Start method to regenerate the pagination.

The Next method is called when the next button is clicked. It increases the Pagination.page value and ensures that it doesn't exceed the total number of pages (Pagination.size). Then it calls the Start method to regenerate the pagination.

The Bind method is responsible for binding event listeners to the generated page number links. It adds a class of "current" to the currently selected page number and attaches a click event listener to each page number link to handle the page navigation.

The Finish method is called after generating the pagination HTML. It sets the innerHTML of the pagination container element (Pagination.e) to the generated code, clears the code property, and binds event listeners to the page number links using the Bind method.

The Start method determines the type of pagination layout based on the total number of pages and the currently selected page. It generates the HTML code for the pagination accordingly using the Add, First, and Last methods. Finally, it calls the Finish method to complete the pagination rendering.

The Buttons method is called during the initialization process. It binds event listeners to the previous and next buttons.

The Create method is responsible for creating the initial HTML structure of the pagination component. It receives an element (e) as a parameter, which represents the container element for the pagination. It adds the required HTML code for the previous and next buttons and a placeholder element for the page numbers.

The Init method serves as the entry point for initializing the pagination component. It receives the container element (e) and data object (data) as parameters. It extends the default pagination data using the Extend method, creates the initial HTML structure using the Create method, and starts generating the pagination using the Start method.

Finally, the init function is called when the DOM content is loaded. It initializes the pagination component by calling the Pagination.Init method with the appropriate container element and data.

Create a JavaScript file with the name of script.js and paste the given codes into your JavaScript file and make sure it's linked properly to your HTML document, so that the scripts are executed on the page. Remember, you’ve to create a file with .js extension.

var Pagination = {

  code: '',

  // converting initialize data
  Extend: function(data) {
      data = data || {};
      Pagination.size = data.size || 300;
      Pagination.page = data.page || 1;
      Pagination.step = data.step || 3;
  },

  // add pages by number (from [s] to [f])
  Add: function(s, f) {
      for (var i = s; i < f; i++) {
          Pagination.code += '<a>' + i + '</a>';
      }
  },

  // add last page with separator
  Last: function() {
      Pagination.code += '<i>...</i><a>' + Pagination.size + '</a>';
  },

  // add first page with separator
  First: function() {
      Pagination.code += '<a>1</a><i>...</i>';
  },

  // change page
  Click: function() {
      Pagination.page = +this.innerHTML;
      Pagination.Start();
  },

  // previous page
  Prev: function() {
      Pagination.page--;
      if (Pagination.page < 1) {
          Pagination.page = 1;
      }
      Pagination.Start();
  },

  // next page
  Next: function() {
      Pagination.page++;
      if (Pagination.page > Pagination.size) {
          Pagination.page = Pagination.size;
      }
      Pagination.Start();
  },

  // binding pages
  Bind: function() {
      var a = Pagination.e.getElementsByTagName('a');
      for (var i = 0; i < a.length; i++) {
          if (+a[i].innerHTML === Pagination.page) a[i].className = 'current';
          a[i].addEventListener('click', Pagination.Click, false);
      }
  },

  // write pagination
  Finish: function() {
      Pagination.e.innerHTML = Pagination.code;
      Pagination.code = '';
      Pagination.Bind();
  },

  // find pagination type
  Start: function() {
      if (Pagination.size < Pagination.step * 2 + 6) {
          Pagination.Add(1, Pagination.size + 1);
      }
      else if (Pagination.page < Pagination.step * 2 + 1) {
          Pagination.Add(1, Pagination.step * 2 + 4);
          Pagination.Last();
      }
      else if (Pagination.page > Pagination.size - Pagination.step * 2) {
          Pagination.First();
          Pagination.Add(Pagination.size - Pagination.step * 2 - 2, Pagination.size + 1);
      }
      else {
          Pagination.First();
          Pagination.Add(Pagination.page - Pagination.step, Pagination.page + Pagination.step + 1);
          Pagination.Last();
      }
      Pagination.Finish();
  },

  // binding buttons
  Buttons: function(e) {
      var nav = e.getElementsByTagName('a');
      nav[0].addEventListener('click', Pagination.Prev, false);
      nav[1].addEventListener('click', Pagination.Next, false);
  },

  // create skeleton
  Create: function(e) {

      var html = [
          '<a>◄</a>', // previous button
          '<span></span>',  // pagination container
          '<a>►</a>'  // next button
      ];

      e.innerHTML = html.join('');
      Pagination.e = e.getElementsByTagName('span')[0];
      Pagination.Buttons(e);
  },

  // init
  Init: function(e, data) {
      Pagination.Extend(data);
      Pagination.Create(e);
      Pagination.Start();
  }
};

var init = function() {
  Pagination.Init(document.getElementById('pagination'), {
      size: 30, // pages size
      page: 1,  // selected page
      step: 3   // pages before and after current
  });
};

document.addEventListener('DOMContentLoaded', init, false);

Final Output:

JavaScript Pagination Simplifying Web Content Navigation.gif

Conclusion:

In this comprehensive guide, we explored the world of JavaScript pagination and its significance in web development. By implementing pagination techniques, you can enhance the user experience, improve performance, and make large datasets more manageable for your users.

We began by discussing the benefits of using pagination in web applications. From enhancing user experience to optimizing resource utilization, pagination offers numerous advantages that can greatly impact the usability of your website or application.

Next, we delved into the basic concepts of JavaScript pagination. Understanding how to determine the total number of pages, displaying a subset of results per page, and implementing navigation controls is crucial for successful pagination implementation.

We then provided a step-by-step guide to help you implement JavaScript pagination effectively. From setting up the HTML structure to writing the necessary JavaScript code and binding event listeners, each step was explained in detail to ensure a smooth implementation process.

By mastering JavaScript pagination, you have the power to simplify web content navigation, present information in a more user-friendly manner, and improve the overall user experience of your web applications.

Now it's time to apply what you've learned and start implementing pagination in your own projects. Experiment with different techniques, explore further possibilities, and always keep the user in mind.

Remember, pagination is not just about dividing content into pages; it's about creating a seamless and engaging experience for your users. So go ahead, embrace JavaScript pagination, and take your web applications to new heights!

Happy coding!

That’s a wrap!

I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post