Live DemoDownload Source Code

Getting Started

We need to first create the index.html and styles.css files in the same directory. I also downloaded a recent copy of jQuery to store with my other JS document. jQuery is the only dependency for what we need to build and the minified library does not take up much space.

As you can see, the header does not contain anything too out-of-the-ordinary. The body content is also easy to understand and I have tried to keep the interface as simple as possible. There is a single div using the ID #documentcontainer which holds the first page image. This img tag will auto-update once the user clicks on either of the navigation links, or clicks on the image itself to move forward one page.

I am only using the → and ← entities for displaying arrow links. But you could update this design to appear any way you like. The really important part would be the IDs attached onto each link. These correspond to jQuery event handlers which trigger after the user clicks on either of the arrows to move forward or back in the document set.

Configuring the Page

One interesting attribute you will find on the img tag is called data-page. We use this attribute to determine which page is currently being displayed without needing to guess from the current filename. jQuery will update this value when switching between alternate pages.

From the CSS styles we really only need to be concerned about the inner body content. You will find all of the typical resets and centered wrapper div styles within the stylesheet. This is all fairly common and I hope frontend developers would be familiar with these ideas already.

The document container has a bit of padding on either side to keep the image centered on the page. Also the img tag itself has the property cursor: pointer; for whenever the user hovers to promote a clicking event. There is no anchor link wrapping the img because we do not want to link anywhere. But the image should be clickable, and this hand cursor mouse icon is the perfect user experience technique.

With everything else setup on the page we can now delve into the jQuery. I tried to keep the script very malleable using variables which you can update for your own documents. But since there are so many different methods to achieve this, I can’t guarantee my code is the absolute perfect end-all-be-all solution!

jQuery Paging Variables

Each individual document page is actually a JPG image exported from this PDF document using Adobe Photoshop. This process should be possible with any typical PDF, which makes publishing on the web using a JS document viewer even easier. We should take a look at my custom file docview.js and break down each of the code blocks.

These 4 variables may be customized if you are using a different set of documents. First is setDirectory which should lead to a local or absolute URL value, with the forward slash, pointing to the directory of the images. fileTypes would be the extension of your document images. lastDocNum is the total count of pages in your set, that way jQuery knows when we have reached the end of your document.

The last variable fileDigits is important to explain. This script assumes you will be labeling document sets in the order of 01.jpg, 001.jpg, or 0001.jpg, using however many zeros to fill the place of pages. If you have a 2,500 page document then the ordering should be numerically sequential and the fileDigits variable would be set to 4 because there are 4 numerical digits within each image name.

Handling Dynamic Images

When I was digging through Stack Overflow I found this amazing solution for appending zeros into the filename. We need to convert the filename into an integer to increase the value by 1. Then we convert it back to a string, add the proper number of zeros and give it the ending file extension.

You probably don’t need to know exactly how this works but it is a great function. The final large block of code will manage jQuery event handlers for clicking on the image, or either of the two navigation links. We setup the main img into a JavaScript variable named docimage and then manipulate the attributes from there.

The new variables created inside will hold the next numerical page number, along with the new page ID number and the new image source. We first check if the ID is equal to the very final number of documents and if so we stop the script from executing further. This means we have hit the final page and the user is clicking on the final page to move on, but we have nothing else to display! Otherwise the img attributes are updated and the new image will display instantaneously.

The last event handler is using jQuery selectors for both arrow links together. Based on the ID name we check if the current page ID should be subtracted or added by one. Also we have to perform a similar check to see if the user is at the very first page and can’t go back anymore, or if they’re at the last page and can’t go forward anymore. Otherwise the codes are almost identical and you should be able to follow all of it through until the end.

Live DemoDownload Source Code

Final Thoughts

I do hope this tutorial may prove useful to some web developers. This idea is still fairly new and there just isn’t a major demand for this functionality on all websites. But I do like the idea of pulling new image URLs dynamically so that the page does not refresh.

This gives the user a cleaner experience and the whole website loads fewer HTTP requests per page. If you have any similar ideas or questions on the tutorial feel free to share with us in the discussion area below!

For more related resources: