Advertisement
  1. Code
  2. JavaScript

How To Create A 'Mootools Homepage' Inspired Navigation Effect Using jQuery

Scroll to top
7 min read

As you know there are a host of competing javascript libraries around these days. Though I prefer jQuery, I've always liked the way the menu on MooTools worked. So in this tutorial we'll recreate that same effect ... but we'll do it in jQuery!


Demo and Source Code



Step 1

Let's begin by writing the necessary HTML to create a simple vertical navigation. For the structure, as you may have guessed, we will use an unordered list <ul> with the ID name "sliding-navigation". Also, we will add some links and give each list item <li> the class name "sliding-element".

I'm also going to add in a title element. This is a useful thing to do as many WordPress blogs for example have title elements in their sidebar navigation (e.g. "Archives"). So it would look something like this:

1
2
<ul id="sliding-navigation">
3
	<li class="sliding-element"><h3>Navigation Title</h3></li>
4
	<li class="sliding-element"><a href="#">Link 1</a></li>
5
	<li class="sliding-element"><a href="#">Link 2</a></li>
6
	<li class="sliding-element"><a href="#">Link 3</a></li>
7
	<li class="sliding-element"><a href="#">Link 4</a></li>
8
	<li class="sliding-element"><a href="#">Link 5</a></li>
9
</ul>

Step 2

Now, let's create a HTML document where we can put the work we just did. Create a new HTML file and call it demo.html. Then open this file with your favorite text editor and insert the following code:

1
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 

3
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
<html>
5
	<head>
6
		<title>Navigation Effect Using jQuery</title>
7
		<link rel="stylesheet" type="text/css" href="styles.css" />
8
		<script type="text/javascript" src="jquery.js"></script>
9
		<script type="text/javascript" src="sliding_effect.js"></script>
10
	</head>
11
	<body>
12
		<div id="navigation-block">
13
            <ul id="sliding-navigation">
14
                <li class="sliding-element"><h3>Navigation Title</h3></li>
15
                <li class="sliding-element"><a href="#">Link 1</a></li>
16
                <li class="sliding-element"><a href="#">Link 2</a></li>
17
                <li class="sliding-element"><a href="#">Link 3</a></li>
18
                <li class="sliding-element"><a href="#">Link 4</a></li>
19
                <li class="sliding-element"><a href="#">Link 5</a></li>
20
            </ul>
21
        </div>
22
	</body>
23
</html>

There are a few things to note here:

  1. The !DOCTYPE for our demo.html file is set to XHTML 1.0 Strict. This is not required for the effect to work but I try to get in the habit of using it as much as I can.
  2. You may have notice that the <link> tag is refering to a file called style.css. However, no such file exists. No worries though, that is the next step.
  3. Finally I've wrapped my navigation block into a <div>. We'll make use of this later to position the block on the page.

Step 3

Now that we have our HTML file labelled and working, let's add some styles. Remember that our HTML document is pointing to a CSS file called styles.css. So, let's begin by creating a file called styles.css and saving it in the same directory where our HTML document lives. As we did in the previous step, open this file with your favorite text editor and insert the following code:

1
2
body 
3
{
4
	margin: 0;
5
	padding: 0;
6
	background: #1d1d1d;
7
	font-family: "Lucida Grande", Verdana, sans-serif;
8
	font-size: 100%;
9
}
10
11
ul#sliding-navigation
12
{
13
	list-style: none;
14
	font-size: .75em;
15
	margin: 30px 0;
16
}
17
18
ul#sliding-navigation li.sliding-element h3,
19
ul#sliding-navigation li.sliding-element a
20
{
21
	display: block;
22
	width: 150px;
23
	padding: 5px 15px;
24
	margin: 0;
25
	margin-bottom: 5px;
26
}
27
28
ul#sliding-navigation li.sliding-element h3
29
{
30
	color: #fff;
31
	background: #333;
32
	border: 1px solid #1a1a1a;
33
	font-weight: normal;
34
}
35
36
ul#sliding-navigation li.sliding-element a
37
{
38
	color: #999;
39
	background: #222;
40
	border: 1px solid #1a1a1a;
41
	text-decoration: none;
42
}
43
44
ul#sliding-navigation li.sliding-element a:hover { color: #ffff66; }

Step 4

At this point your demo.html page should be looking something like this:

Demo PreviewDemo PreviewDemo Preview

So, it is finally time to begin using jQuery. But before we can get started we need to do a few of things:

  1. Download the latest version of jQuery.
  2. Create a new file called sliding_effect.js and save it in the same directory as that of your HTML and CSS file.
  3. Lastly, insert the two previous files to your HTML document's <head>.

This is what your HTML file's <head> should look like now:

1
2
	<head>
3
		<title>Navigation Effect Using jQuery</title>
4
		<link rel="stylesheet" type="text/css" href="styles.css" />
5
		<script type="text/javascript" src="jquery.js"></script>
6
		<script type="text/javascript" src="sliding_effect.js"></script>
7
	</head>

Step 5

Now, we will create the function that will do all the "heavy" lifting for the sliding effect to work. This function will take five parameters (navigation_id, pad_out, pad_in, time, and multiplier) and use them as follows:

  1. navigation_id: This is the ID name of the navigation, which contains the elements the effect will be applied on.
  2. pad_out: This is the number of pixels to be padded left when one of the links inside the navigation is hovered.
  3. pad_in: This is the number of pixels to be padded left when one of the links inside the navigation is no longer being hovered.
  4. time: This represents the amount of time (in milliseconds) that takes for one of the link elements to slide in and back in to place when the page is loaded.
  5. multiplier: The job of the multiplier is to increase or decrease the amount that takes the a following link element to slide in to the screen. In other words, if the multiplier is 1, all link elements will slide in to the screen in equal time intervals. However, if it is less than 0, the subsequent link elements will slide in faster, and if it is more than 1 the opposite will happen.

So, open your sliding_effect.js file and insert the following code:

1
2
function slide(navigation_id, pad_out, pad_in, time, multiplier)
3
{
4
	// creates the target paths

5
	var list_elements = navigation_id + " li.sliding-element";
6
	var link_elements = list_elements + " a";
7
	
8
	// initiates the timer used for the sliding animation

9
	var timer = 0;
10
	
11
	// creates the slide animation for all list elements 

12
	$(list_elements).each(function(i)
13
	{
14
		// margin left = - ([width of element] + [total vertical padding of element])

15
		$(this).css("margin-left","-180px");
16
		// updates timer

17
		timer = (timer*multiplier + time);
18
		$(this).animate({ marginLeft: "0" }, timer);
19
		$(this).animate({ marginLeft: "15px" }, timer);
20
		$(this).animate({ marginLeft: "0" }, timer);
21
	});
22
23
	// creates the hover-slide effect for all link elements 		

24
	$(link_elements).each(function(i)
25
	{
26
		$(this).hover(
27
		function()
28
		{
29
			$(this).animate({ paddingLeft: pad_out }, 150);
30
		},		
31
		function()
32
		{
33
			$(this).animate({ paddingLeft: pad_in }, 150);
34
		});
35
	});
36
}

Step 6

All we need to do in order to trigger the script is call the function when the page has loaded. There are two place to put the following snippet of code. It can either be written inside the sliding_effect.js file (I chose this option for this tutorial) or called within the HTML using a <script> tag. Either case will use the same code, which is as follows:

1
2
$(document).ready(function()
3
{
4
	slide([navigation_id], [pad_out], [pad_in], [time], [multiplier]);
5
});

Step 7

Finally we'll add a bit of style to the page to make it look slightly more glamourous. First I've created an image block that looks like this:

The image has a faint gradient, a highlight line, is 190px wide and called "background.jpg". The idea will be to position this to the left of our navigation so that the buttons slide in under it. We'll also add a little heading title to the page. So our HTML becomes:

1
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 

3
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
<html>
5
	<head>
6
		<title>Navigation Effect Using jQuery</title>
7
		<link rel="stylesheet" type="text/css" href="styles.css" />
8
		<script type="text/javascript" src="jquery.js"></script>
9
		<script type="text/javascript" src="sliding_effect.js"></script>
10
	</head>
11
	<body>
12
		<div id="navigation-block">
13
        	<img src="background.jpg" id="hide" />
14
            <h2><span>Navigation Effect Using jQuery</span></h2>
15
            <p>By Bedrich Rios</p>
16
            <ul id="sliding-navigation">
17
                <li class="sliding-element"><h3>Navigation Title</h3></li>
18
                <li class="sliding-element"><a href="#">Link 1</a></li>
19
                <li class="sliding-element"><a href="#">Link 2</a></li>
20
                <li class="sliding-element"><a href="#">Link 3</a></li>
21
                <li class="sliding-element"><a href="#">Link 4</a></li>
22
                <li class="sliding-element"><a href="#">Link 5</a></li>
23
            </ul>
24
        </div>
25
	</body>
26
</html>

Notice that I've added the image inside the "navigation-block" element and give it an ID called "hide". Also I've added a heading element and subtitle. Now we add a bit of extra CSS to our styles.css file as follows:

1
2
3
h2 	
4
{ 
5
	color: #999;
6
	margin-bottom: 0; 
7
	margin-left:13px;
8
	background:url(navigation.jpg) no-repeat;
9
	height:40px;
10
}
11
12
h2 span
13
{
14
	display: none;
15
}
16
17
p	
18
{ 
19
	color: #ffff66; 
20
	margin-top: .5em;
21
	font-size: .75em;
22
	padding-left:15px;	
23
}
24
25
#navigation-block {
26
	position:relative;
27
	top:200px;
28
	left:200px;
29
}
30
31
#hide {
32
	position:absolute;
33
	top:30px;
34
	left:-190px;
35
}

So first in the <h2> element, we have set the HTML text to vanish using "display:none" and set a background image of some nicer looking text I prepared earlier.

Also notice that the "navigation-block" element now has a relative position, so that we can move the "hide" image over to the left. This will make the tabs appear from under it.

Lastly to give our tabs a bit of finish I've added a subtle background image that looks like shading like this:

1
2
ul#sliding-navigation li.sliding-element h3
3
{
4
	color: #fff;
5
	background:#333 url(heading_bg.jpg) repeat-y;
6
	font-weight: normal;
7
}

Finished

And we're done!

View the Final Effect

Download the HTML/Images/JS

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.