0

Building an Awesome Navigation Menu with jQuery: Part 1

- Freelancer

menu expanded
Do you need to build a navigation interface that has to handle hundreds of product links? All grouped in categories, subcategories? Perhaps even containing thumbnails? If the answer is yes, here’s a usability path to avoid :

  1. select categories, wait for a page to load
  2. select a subcategory, wait for another page to load
  3. check out products
  4. click to go to desired product

The typical web user only wants to click once or twice to get to the product they want. And if clicks are unavoidable, make sure page loads are minimized! The more clicks and the more page loading they have to go through, the higher the risk they will get lost and never come back to your site. The good news is that you can achieve a good navigation menu that reduces page reloads.

This article is an in-depth tutorial on how you can achieve an expandable navigation menu using valid xhtml coding, valid css and a bit of javascript.

The concept

In order to achieve a fully functional menu for our huge website, we need one that will enable users to choose a category or subcategory in a timely manner, then browse hassle-free for the product they like. In Part 1 of the tutorial, we will create a horizontal bar that contains all the main categories of the products our customer is selling. This menu bar will expand so users will see all sub-categories. In Part 2 of the article, we’ll create the functionality where clicking on a sub-category will trigger an overlay that displays a list of the individual products.

menu sketch

In the image above there’s a sketch of what the site layout should be. We have the menu bar, right under the menu comes a header that can contain images, paragraphs of text, news. You get the picture. Then comes the content area. The menu will expand and push the header together with all elements that come after it down the page, as you can see in the following image.

menu expanded

Sneak peak at what we’re building here

Just in case you cannot wait for the end of the article, just check out the demo over here.

Coding the HTML and CSS

The navigation bar will consist of an unordered list. I have chosen this tag because menus contain lists, therefore an unordered list is the best way to go for this. The part of the menu that will slide will be contained by a div, each item in the navigation menu having a separate div for its information. We will have the header and the content section also contained in a div. Here’s a view on the XHTML structure.

I chose to round up the corners of the menu. To create this effect we will use some CSS level 3 and browser specific markup. The navigation bar will also have vertical separators between the items. The separator is consisted of two different lines, each with a custom color. We won’t use images for the separators. Instead we will use the css border property. Each list item in the navigation bar will have a 1px solid left border, and a 1px solid right border. The first element in the list and the last one won’t have the left border, and right border respectively. Here’s the html markup of the modified list, to reflect that there won’t be the above mentioned borders, and the css code:


Because I chose to round the corners of the menu, there are some issues while hovering the menu items, so the links that are held by the li items will have to have rounded corners as well. The first link in the menu will have rounded corners on the left side, while the last one will have rounded corners on the right side. Here’s the code for that:

Coding the behavior of the menu using jQuery

I am not going to reinvent the wheel here, but jQuery seems to be the best javascript framework out there. If you’re not familiar to jQuery you can find some great tutorials and examples over here.

Javascript for menus? Are you insane!

Some people out there say you must not use javascript for navigation under any circumstances. I am sorry guys, but if you write your code professionally, using javascript for menus can seriously enhance your site’s functionality. The trick they’re talking about is what do you do when javascript is unavailable in the visitors browsers? Well, you have to code your menu in such a way it has progressive enhancement and works just fine if javascript is off. And yes, this menu achieves progressive enhancement. If javascript is turned off, the menu will be directly visible, including the part that should slide when needed. Nothing is inaccessible. Moreover, search engine’s bots will see the site just as if they were a visitor without javascript, so search engine optimization is preserved if using this menu.

Let’s get down to the jQuery

jQuery uses html IDs and classes to identify elements in the html page that it needs to work with. Our menu currently has unique ID, but the “cleaning”, “cooking” and “gardening” links don’t, so we add a class for each of them. The classes are unique too.

In order to achieve progressive enhancement, we must first disable javascript and load the menu page. So far, due to our code the menu displays in its entirety, including the part that in a few minutes will slide beautifully if javascript is on. But if it has to slide, it has to be hidden first, and this is the trick we pick out from our sleeves. The first thing we’re to do with jQuery is to hide the sliding part of the menu. By doing this, people having javascript turned on will not see the menu until they click to expand it. They will only see the menu bar. Here’s the jQuery code needed to achieve this:

The rest is simple. When you click one item in the navigation bar, you are taken to a page. This happens for the Home and About us links. When you click one of the links in the middle of the menu, the menu will slide and reveal the contents. If there’s another menu visible, clicking to expand one other item will make the expanded section slide back under the navigation bar, then the required content will slide back out. How cool is that?

jQuery code step by step
There’s a bit of jQuery to be written here, so you can take a peak below to the full source code, then continue to the part I dice the code in pieces and explain what I’ve done.

The first jQuery code is $(’.clear’).height(0);. This is used to set the height of the clearing div to 0. We added a clearing div to each of the expanded menu’s divs because all content inside them floats to the left, and would therefor cause the expanded menu div to collapse. By adding the clearing div we avoid that, but we also add 20 unnecessary pixels to the expanded menu’s height. We fix this problem with the code mentioned above.

Then, we have three different onclick functions. Each one does the same thing, but is triggered by a different element in the menu. I will explain the first one. $(’.cleaning-but’).click(function () is used to trigger clicking on the href tag that contains the link to cleaning menu. That link is identified by the cleaning-but class we added previously. When that link is clicked we don’t have just one behavior, so everything has to go inside a function.

var isVisible = $(’.menu-extend’).is(’:visible’); is used to check if there’s any menu currently expanded. The variable will hold either true or false values. Here, the expanded menu is identified by the general class used for it menu-extend. We will use the same process to check what expanded menu is in fact expanded, be it the one for cleaning category, or another one. We will identify those by their unique classes cleaning, cooking and gardening.

We first check if the menu is visible. if(isVisible) will return true if there’s an expanded menu, or false otherwise. If false, we will simply slide down the menu using $(’.cleaning’).slideDown(); located on the else side of the if. If there’s an expanded menu, we check to see if it is exactly the one we clicked. If it is the same one, we will slide it back up using $(’.cleaning’).slideUp(”1000″);. If it is another one, we slide up that div using a similar piece of code, then slide down the one needed. $(’.cooking’).slideUp(”1000″, function() { $(’.cleaning’).slideDown(”1000″) }); slides up the cooking expanded menu, and after that process is completed, it slides down the cleaning menu. The syntax for such a process is $(’identifier’).slideUp(duration, callback event – the one triggered after first one is completed);

Here’s a live demo version of the menu. Zip archive download will be available once we publish the second part of this article!

In the concept part we said that the contents of the expanded menu are in fact subcategories, and clicking on a subcategory will reveal the products located in that category. But that is another story and we will discuss it in the second part of the article.

Coming up next
Part 2: Displaying menu items using overlay

0

How much do I charge for Graphic Design Freelancing?

- Freelancer
freelancing
Matt asked:


I just got my first real freelancing job. I have to do some magazine advertisments and mail fliers and inserts? Could anyone tell me how much they charge? A ballpark figure?

Caffeinated Content
0

Any suggestions for setting up a small freelancing advertising business?

- Freelancer
freelancing
Justin G asked:


I am a senior with a minor in advertising, and am looking to possibly set up my own small freelancing advertising business just to make some extra cash and experience. Any suggestions…costs, how much to charge, where to start, etc.?

Thanks

Caffeinated Content for WordPress

0

What is the best and most effective way of making money on the internet?

- Freelancer
internet freelancing
mrcharlie69 asked:


I would like to know what is the very best of making money on the internet. I am a graduate student, and I am looking for the best method of making money consistanly (like on a job) on the internet. I have tried eBay, Online Freelance writing, Online Survays, and OBS-Online Business Systems. Which is the best method of making money? (And please, no B.S)

Caffeinated Content
0

Usability Tips: How to Properly Handle Images in your Site

- Freelancer

One of the most important aspects of the overall feel of a website comes from the pictures that lie within it. It is great that you can provide great layouts, interesting and enhanced navigation, valuable content, but all of the above will be worthless if the pictures you insert next to your content are:

  • of awful, low quality
  • displayed in the worst way possible

The first issue is usually solved by using stock photos. There’s a bunch of free stock photo galleries and there’s even more with paid subscription. However, the second issue is tricky. In the past years you have come to know quite a few css/js powered image display tools like LightBox. Then there’s ThickBox, which I consider somewhat nicer. Both can be used in order to enlarge small thumbnails, but they both seem odd. Moreover, it depends on how you display the thumbnail, as some might not know immediately that clicking the image will provide a full size screen shot.

Brown's website

Brown

Take for example Brown University’s website in the screen shot. If you hover the images in the post, you will see a “+ Zoom” message in the upper left corner of the image. You will then try to go to that corner, and unfortunately, when you reach the “+ Zoom” button, it will start blinking (tested in Safari and Firefox). Now, that is really confusing. Not to mention that you can click anywhere in the picture to see the full size version. But oops, you do not know that.

Don’t panic, because I think I have found the best solution out there, and I am sharing this with you. If you check the latest version of Media Temple’s website, they too have screen shots in their control panel FAQ section. And those images are displayed perfectly. Rounded corners, borders, captions. But that’s not all of it. The great is about to be revealed. If you hover the image, in any given point, your pointer transforms into a “+” magnifying glass. What comes more natural to “enlarge” than a magnifying glass? Nothing! You even have text annotation, “Click to enlarge”, stupid.

Media Temple's website screenshot, general

Media Temple

Clicking will reveal a full size photo, more annotations, and in the upper central area of the image, navigation controls. YES! No need to scroll for next, previous etc buttons down to the bottom of your screen. You can even move the photo around, start slideshows, go to next picture, enlarge it even more and obviously hit the “X” close button. Now, if you do not want to go with the mouse around your screen to the “X” button, you will see your mouse pointer transformed into a “-” magnifying glass. Now, what comes more natural to “zoom out” than a magnifying glass with a minus inside it? Nothing!

Media Temple's website screenshot, zoomed in

Media Temple

Media Temple uses HighSlide JS, which is released under commercial license, but they do have a free version for you to try out.

Hands down for Media Temple!

Do you have a favorite or unique example of image handling you’d like to share?  Let us know via the comments section below.

If you liked this article, please subscribe to our RSS feed and social bookmark this post using the links below.

0

Where can I write freelance articles on the internet and get paid every time?

- Freelancer
internet freelancing
Gerri R asked:


There are some sites that make it a competition. Two or more people write an article on the same thin and they chose their favorite one and publish it. That is the only person who gets paid. The other person(s) wasted their time writing their article and they don’t get published or paid at all.
I want a site that will publish my articles EVERY TIME I write one.
How much do they usually pay you for each article?

Caffeinated Content