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 a Tabbed Ajax Content Widget using jQuery

November 19, 2013 by Jake Rocheleau

Blogs are usually full of different posts all with varying degrees of popularity. Readers don’t always know how to find related posts without using the search feature. It helps to build smaller widgets right into the page that can automatically sort through content based on different criteria.

In this tutorial I want to demonstrate how we can build a sidebar-based tabbed ajax widget to display related blog posts. Many times these widgets feature some type of thumbnail image, along with other metadata like the author and publication date. I’ve kept things real simple mostly focusing on the jQuery animations. Take a peek at my live demo to understand what the final outcome should be.

ajax sidebar posts widget jquery tutorial screenshot

Live Demo – Download Source Code

Getting Started

First I am creating a new blank HTML5 document with a couple related files. The first is a local copy of jQuery and the second is a styles.css stylesheet. This will include some page resets along with the generic layout formatting.

<!doctype html>
<html lang="en-US">
<head>
  <meta charset="utf-8">
  <meta http-equiv="Content-Type" content="text/html">
  <title>Tabbed Ajax Content Widget - SpyreStudios Demo</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" media="all" href="css/styles.css">
  <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
</head>

The overall webpage is built to appear like a very simple weblog. The left-hand content uses some filler Ipsum to look like blog posts. While in the sidebar you will find a small widget using 3 top links organizing newest, popular, and random articles.

    <div id="tabbed-posts">
      <header class="navlinks">
        <a href="#" id="newpostslink" class="active">Newest</a>
        <a href="#" id="popularpostslink">Popular</a>
        <a href="#" id="randompostslink">Random</a>
      </header>

I really like this design because it is sleek and conforms nicely into any layout. It would not require much effort to extend the design into more colorful examples to fit your own project. We also use a coordinated CSS class of .active to check which tab of content is being displayed at any given time.

CSS Page Styles

Aside from my typical resets I have also included a new Google Webfont named Crete Round for our headings. The div IDs #left and #sidebar are both set to fixed widths floated within a clearfix container. This also means the widget itself has been set to a fixed width. It should be noted that mobile users may not even want to use this widget, and you might consider hiding it beyond a certain device width using media queries.

#w {
  display: block;
  margin: 0 auto;
  width: 800px;
  padding: 11px 16px;
  background: #fff;
  border: 7px solid rgba(0,0,0,0.35);
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
}
#left {
  display: block;
  float: left;
  width: 460px;
  padding: 0 7px;
  margin-right: 14px;
}

#sidebar {
  display: block;
  float: left;
  width: 280px;
}

The actual navigation links are centered in their own heading element. If you wanted to use tabs it would require some type of border, or possibly a box shadow surrounding the link. I’ve instead opted to use a simple background effect on hover which sticks to the active link. Also the full tabbed posts widget is fixed at 250px width for the sake of this demo.

/** tabber widget **/
#tabbed-posts {
  display: block;
  width: 250px;
  margin: 0 auto;
  padding-top: 25px;
}

#tabbed-posts .navlinks {
  display: block;
  padding: 14px 0;
  font-size: 1.4em;
  text-align: center;
}
#tabbed-posts .navlinks a {
  text-decoration: none;
  font-weight: normal;
  margin-right: 5px;
  color: #588bc4;
  padding: 5px 7px;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
}
#tabbed-posts .navlinks a:hover, #tabbed-posts .navlinks a.active:hover {
  background: #dde8f4;
}

#tabbed-posts .navlinks a.active {
  font-weight: bold;
  background: #e8f0f9;
}

Now this block of code shouldn’t come as any surprise. Everything can be styled nicely if we include floating elements with a clearfix container. The biggest difference comes within each content section of the page. I am using three divs with the class .tabcontent and they each contain an unordered list of links.

.tabcontent ul li {
  padding-bottom: 5px;
  padding-top: 7px;
  border-bottom: 1px solid #cbcbcb;
}

.tabcontent ul li h3 {
  font-size: 1.5em;
  line-height: 1.4em;
  font-weight: normal;
}

.tabcontent ul li h3 a {
  display: block;
  width: 100%;
  color: #6a6e7a;
  padding: 3px 0;
  font-weight: bold;
  text-decoration: none;
  text-shadow: 1px 1px 0 #fff;
}
.tabcontent ul li h3 a:hover {
  background: #dde8f4;
}

.tabcontent ul li h3 .featuredpic {
  display: block; 
  float: left;
  margin: 0;
  margin-right: 3px;
  padding: 3px 4px;
}
.tabcontent ul li h3 .featuredpic img {
  display: block;
}

The first div is always displayed on pageload because it focuses on the “newest” posts. As you click between links it’ll auto-hide the main content in order to show the new list. Generally this would require additional classes but I’ve opted to instead write inline CSS styles for display:none. This helps when dealing with jQuery’s fadeIn() and fadeOut() methods that use inline styles anyways.

Tabbed Content with jQuery

In any website this widget is basically useless without some JavaScript to handle the dynamic content switching. It isn’t too overly-complicated so anybody with some familiarity in jQuery should be able to follow along. I have broken down my script into two blocks of code so that it is easier to understand.

$(function(){
  $('#tabbed-posts .navlinks a').on('click', function(e){
    e.preventDefault();
    
    if($(this).hasClass('active')) {
      return;
    } 

When somebody clicks on any of the anchor elements within .navlinks we first prevent the default action from loading. We simply do not want the HREF value loaded into the address bar. I’m then checking if the clicked link already has a class of .active then we know it’s already displayed and we don’t need to do anything. If the clicked link is already active then the jQuery return command will end the function immediately without processing any further codes.

    else {
      var currentitm = $('#tabbed-posts .navlinks a.active').attr('id');
      if(currentitm == 'newpostslink') { var currentlist = $('#tabbed-posts-new'); }
      if(currentitm == 'popularpostslink') { var currentlist = $('#tabbed-posts-popular'); }
      if(currentitm == 'randompostslink') { var currentlist = $('#tabbed-posts-random'); }
      
      var newitm = $(this).attr('id');
      if(newitm == 'newpostslink') { var newlist = $('#tabbed-posts-new'); }
      if(newitm == 'popularpostslink') { var newlist = $('#tabbed-posts-popular'); }
      if(newitm == 'randompostslink') { var newlist = $('#tabbed-posts-random'); }
      
      $('#tabbed-posts .navlinks a').removeClass('active');
      $(this).addClass('active');
      
      $(currentlist).fadeOut(320, function(){
        $(newlist).fadeIn(200);
      });
    } // end if/else logic
    
  }); // end event handler
});

Otherwise the link is not active and we begin the transition process. My widget is fairly circumstantial based on IDs I’ve written into the code myself. You should update these ID values based on your own code samples and which labels can work well for your project. currentitm is a variable targeting the current link while currentlist targets the currently-active container div .tabcontent, which each use their own unique identifier.

We then generate new variables which should be displayed after the animation finishes hiding the old content. newitm pertains to the new link while newlist targets the new div container. The switching process is very easy by using addClass() and removeClass() coupled with the jQuery fading methods.

One point worth noting is the fadeOut() method has to complete before the fadeIn() animation will be called. To ensure this happens I have written a callback function which only runs after the fade out completes. Then inside this callback function we can execute the display of our new tab content list, all of which should complete animating within less than 60 seconds.

ajax sidebar posts widget jquery tutorial screenshot

Live Demo – Download Source Code

Closing

I usually love finding these smaller related widgets in sidebar designs because they show just how much is possible in modern blog layouts. CMS engines like WordPress have open APIs that allow you to build these widgets from custom-coded SQL queries. I have been really impressed with modern design trends and I would expect these widget ideas to continue advancing further into the future.

Filed Under: Tutorial Tagged With: howto, jquery, open source

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