How to Create a Sleek Navigation with a Sliding Backgound Using jQuery
Accessibility should be your number one priority when creating a website regardless of it’s size, audience, or client. However, it should never be a constraint on your creativity. There are many ways to get web-creative without compromising any of you users’ experience. In this tutorial, we will target the navigation, which tends to get a lot of attention given it’s prominent position, and create a sliding background effect. Here is a demo of what we will be aiming for, hover over the navigation to see the effect in action.
Step 1
Let’s begin by creating the necessary and most basic HTML to get the page up and running. Regardless of what else you have in the page, this script will require one ul with the id navigation and jQuery, either download a copy or link to the latest version (as I’m doing below). So, right now you should have something that looks more or less like this:
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <title>Sliding Nav Background</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://code.jquery.com/jquery-latest.js"> </script></head> <div id="banner"> </div> <!-- step 1 starts here --> <div id="content"> <ul id="navigation"> </ul> </div> <!-- ends here --> </body> </html>
Step 2
Now that we have the HTML nice and ready, let’s add some style. The following only demonstrates the CSS that pertains to the navigation, take a look at the source code for the rest of the rules.
#navigation { list-style: none; font-size: .8em; width: 150px; margin: 0; padding: 10px 0; border-left: 1px solid #2e2e2e; background: url('shadow.jpg') top left repeat-y; } #navigation li { margin-bottom: 30px; } #navigation li a { color: #FF77CC; text-decoration: none; padding: 5px 10px; display: block; width: 140px; } #navigation li:last-child { margin-bottom: 0; } #navigation li a:hover { color: #fff; text-decoration: underline; } #slider { background: #444; display: none; position: absolute; z-index: -1; -moz-border-radius: 0 10px 10px 0; -webkit-border-radius: 0 10px 10px 0; }
Note: If you are an observant person, you might have notice that there is no element by the id slider in the HTML shown above; but worry not, you will find where that is coming from on the next step of this tutorial.
Step 3
This step requires the use of non-semantic span but worry not web standards believers (I am one myself), we will add this span with the help of jQuery. So, first we will need to target the ul containing our navigation, give it the same width and height of our link elements (remember to add total padding), and hide it. This is what you should have so far:
$(document).ready(function() { $("#navigation").after("<span id='slider'></span>"); var slider = $("#slider"); slider.width($("#navigation li a").width() + 20); slider.height($("#navigation li a").height() + 10); slider.hide(); }
Step 4
Now that we have our slider appended, we can create the script that will provide some eye candy for our JavaScript enabled users. The main idea is to have the newly created slider follow a user’s mouse whenever they move about the navigation. So, we will use the mousemove event to follow the user’s mouse and get it’s current coordinates by utilizing pageX and pageY on the element which was passed to the mousemove event function (”position” in this case). Here is what this should look like:
$("#navigation").mousemove(function(position) { slider.show(); slider.css("left",$("#navigation").offset().left); slider.css("top",position.pageY - 10); });
Note: I have subtracted 10 from the “top” to compansate for the links’ total vertical paading and to situate the backgeound right on the middle.
Step 5
Although you could now combine the jQuery done on step 4 and 5 and have a working navigation with a sliding background, there is one more thing to account for, the navigation’s total vertical padding which we applied on the CSS. For this we are going to need an if statement that will prevente the sliding background from leaving our padded navigation. For this step we only need to consider the top padding and the height of the navigation. So, here is what the script looks like we all the parts included:
$(document).ready(function() { $("#navigation").after("<span id='slider'></span>"); var slider = $("#slider"); slider.width($("#navigation li a").width() + 20); slider.height($("#navigation li a").height() + 10); slider.hide(); $("#navigation").mousemove(function(position) { if(position.pageY > $("#navigation").offset().top + 10 && position.pageY < ($("#navigation").offset().top + $("#navigation").height())) { slider.show(); slider.css("left",$("#navigation").offset().left); slider.css("top",position.pageY - 10); } }); });
Read Me
Feel free to use or alter this technique in any of your projects be it personal or commercial. I don’t ask for anything unless you of course you decide to copy the code in this tutorial entirely and call it your own. However, a link back (www.bedrichrios.com) has never hurt anyone or anything. Enjoy!


