SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

How To Build an Alphabetical Page Index using jQuery

December 20, 2012 by Jake Rocheleau

Most web developers are probably familiar with the anchor link solution for jumping to sections of the page. You can setup anchor links with a specific name attribute, and use the hash symbol as an href value to skip down the page. This effect has worked fantastic when listing very long sets of data. However the page jump isn’t exactly elegant and can sometimes throw off visitors, leaving them confused.

In this tutorial I want to explore a solution to this interface method. We will be creating a simple page index which uses anchor links as stepping stones on the page. However the “jump” will be animated and scroll down to reach the destination. I particularly enjoy this script because it is portable and easy to install into any website layout.

preview demo screenshot jQuery JavaScript scrolling index page links

Live Demo – Download Source Code

Spec the Layout

We can start off by creating the initial pages needed to get this script running. First is the basic index.html page which I am adding a typical HTML5 doctype. We also need to create two blank documents used later in the tutorial – styles.css for the page stylesheet and indexscroller.js for our custom jQuery codes.

In the document header I am also including the most recent version of jQuery from Google’s CDN hosting. Also a copy of the html5shiv trunk library for older browsers which do not support HTML5. In the body section I am using a custom Google Webfont named Milonga, along with some artistic CSS3 effects.

<!doctype html>
<html lang="en-US">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>jQuery Alphabetical Scrolling Links Index</title>
  <meta name="author" content="Jake Rocheleau">
  <link rel="shortcut icon" href="http://spyrestudios.com/favicon.ico">
  <link rel="icon" href="http://spyrestudios.com/favicon.ico">
  <link rel="stylesheet" type="text/css" href="styles.css">
  <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Milonga">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script type="text/javascript" charset="utf-8" src="indexscroller.js"></script>
<!--[if lt IE 9]>
  <script type="text/javascript" src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>

Now the page layout will be styled as just a single column with TV show listings. Each show has its own div which contains a photo and some external links to Wikipedia and IMDB. At the very top of the page you’ll find a small box of links which are labeled A-G. These links will behave as our page index which scrolls down to the very first instance of that letter.

Main Body Contents

The entire body area is contained inside a wrapper div, which is split up by the header and TV show listings. The anchor link HREF values target indexes on the page which are listed alphabetically. So we start at #indexa ending with #indexg which are all targeting other anchor links on the page, using matching values for the name attribute.

  <div id="w">
    <h1>Dynamic jQuery Scrolling Links Index</h1>
    
    <div id="container">
      <nav id="links">
        <ul class="clearfix">
          <li class="label">Quick Links:</li>
          <li><a href="#indexa">A</a></li>
          <li><a href="#indexb">B</a></li>
          <li><a href="#indexc">C</a></li>
          <li><a href="#indexd">D</a></li>
          <li><a href="#indexe">E</a></li>
          <li><a href="#indexf">F</a></li>
          <li><a href="#indexg">G</a></li>
        </ul>
      </nav>

We can look at one example of the inline anchor links which can be found in-between TV show listings. I have left these anchors outside of any container since they don’t actually appear in the layout. These are in place right at the beginning of each new letter so that jQuery knows exactly where to stop scrolling.

<div class="show">
  <h2>Arrested Development <span class="meta"><a href="http://en.wikipedia.org/wiki/Arrested_Development_(TV_series)" target="_blank">Wikipedia</a> - <a href="http://www.imdb.com/title/tt0367279/" target="_blank">IMDB</a></span></h2>
  <p><img src="images/arrested-development.png" alt="Arrested Development TV Show" width="570" height="280"></p>
</div>

<a name="indexb"></a>
<div class="show">
  <h2>The Big Bang Theory <span class="meta"><a href="http://en.wikipedia.org/wiki/The_Big_Bang_Theory" target="_blank">Wikipedia</a> - <a href="http://www.imdb.com/title/tt0898266/" target="_blank">IMDB</a></span></h2>
  <p><img src="images/big-bang-theory.png" alt="The Big Bang Theory TV Show" width="570" height="280"></p>
</div>

Notice that before moving into the new letter, I have placed a single anchor using nothing more than its name attribute. We do not need any internal text or href value or anything else. Since we are following semantic HTML the user could also have JavaScript turned off and the fallback would still work properly, just like any generic anchor links.

CSS Page Styles

Some of the default stylesheet contents are really unique and helpful in this situation. Aside from the typical CSS resets I have also placed styles for positioning the body properly. I am using a small CSS3 box shadow on the body container so the layout stands out off the BG texture.

/* layout display styles */
#w { width: 620px; margin: 0 auto; padding-top: 55px; }

#container { 
  padding: 14px 20px;
  background: #fff;
  -webkit-box-shadow: 2px 2px 1px rgba(0,0,0,0.35);
  -moz-box-shadow: 2px 2px 1px rgba(0,0,0,0.35);
  box-shadow: 2px 2px 1px rgba(0,0,0,0.35);
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}

Now for the TV show boxes I have included a few really important properties. Since the anchor links behave as stopping points in the page, I have included extra padding on the top of each TV show container. This way our scrolling effect doesn’t stop right at the top of each title, and there is some extra whitespace for breathing room.

/* tv shows display */
#shows { display: block; }

.show { display: block; padding-top: 8px; margin-bottom: 23px; }
.meta { font-family: Arial, Verdana, sans-serif; color: #222; font-size: 0.8em; font-weight: bold; float: right; }

/* clearfix */
.clearfix:after { content: "."; display: block; clear: both; visibility: hidden; line-height: 0; height: 0; }
.clearfix { display: inline-block; }
 
html[xmlns] .clearfix { display: block; }
* html .clearfix { height: 1%; }

Also the meta information is contained inside each header block, to save space in HTML markup. So we are floating this content and using a CSS clearfix to keep the layout in structure. With the layout design looking pristine, we should now move onto the final task of scripting the index animation effect.

jQuery scrollTop

This solution came together after searching online for a while and eventually stumbling upon this CSS-Tricks forum thread. jQuery has a method named .scrollTop() which can pull the current value in pixels from the top of the page to any other selected element. Using this technique we can determine the offset value for any TV show listing and naturally scroll down to that exact anchor link.

$(document).ready(function(){
  $('#links > ul > li > a').on('click', function(e){
    e.preventDefault();
    var anchorid = $(this.hash);
    
    if(anchorid.length == 0) anchorid = $('a[name="' + this.hash.substr(1) + '"]');
    else anchorid = $('html');
    
    $('html, body').animate({ scrollTop: anchorid.offset().top }, 450);
  });
});

So this is all the code you will find inside indexscroller.js. It doesn’t appear like too much, and it really isn’t all that complicated to follow. But let’s break down what happens after the DOM has finished loading.

After any of the internal #links anchors are clicked we immediately call e.preventDefault(). This will stop the hash values from appending onto the URL and jumping down the page instantaneously. Then using a new jquery.hash property we can pull the exact href value which comes after the hash symbol. So for example, our first index link would return a value of “indexa”.

Using this new value we can target the corresponding anchor link on the page with a matching name attribute. We setup this new anchorid variable and can now access the absolute number of pixels from the top using anchorid.offset().top. Finally adding all of this code into a simple jQuery .animate() method and we have our working index scroller.

jQuery scrollTop scrolling index anchor links

Live Demo – Download Source Code

Final Thoughts

This trick is perfect for web developers who are looking to increase the friendly-ness of any digital user interface. Websites are still in a very young stage of growth, yet advancing rapidly with the revolution of open source code. I applaud any developers who can put this script to good use, or even add onto it through additional functionality.

You can feel free to check out my live demo above and also grab a copy of the project source code. Everything should be easy enough to implement into your own layout, and all you need to change are the anchor link values. Additionally if you have any questions or comments on the tutorial you may share them with us in the post discussion area below.

Filed Under: Tutorial Tagged With: anchor links, howto, html5, jquery, tutorials, tuts

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022