Advertisement
  1. Code
  2. Web Development

Create a Simple, Powerful Product Highlighter with MooTools

Scroll to top
Read Time: 7 min

Believe it or not, there's a lot of powerful interactivity you can bring to your site using javascript besides slick navigation menus! This tutorial will help you find your inner cow by introducing you to one of the most powerful and modular javascript libraries—MooTools! We'll be creating a flexible tool for highlighting your sites products or services using the MooTools javascript framework. Also, learn some of the many reasons why MooTools just might be the right javascript library for all of your future projects!

The Demo

So here's what we're building, it's a clever rollover mechanism that works really well as a product highlighter.

 

Why MooTools?

I know what you're thinking... What could MooTools possibly have to offer that might cause me to break off my long-standing relationship with jQuery—Or Prototype and Scriptaculous for that matter!

One of the biggest problems I've ran into with using jQuery in the past, is the fact that so many of their plugins are created and developed independently—which MEANS, you're placing your trust in a stranger to be actively updating their plugin as the jQuery library continues to release newer and newer versions as well. When this fails (and often times it does) you'll find yourself searching for the proper version of the core library that'll allow your script to function correctly!

Maybe Prototype and its well known partner in crime, Scriptaculous, are more your style. In this particular case you'll be deprived of your right of modularity, and you're forced to include two beefy sized libraries on all of your pages—or in some cases a plugin file as well!

So if MooTools is so great then... why isn't it used more? Why aren't there gazillions of tutorials and books on every library shelf?! There's a handful of reasons:

  1. MooTools is geared more towards the intermediate to advanced scripter.
  2. You won't find collections of cut and paste plugins that allow for immediate implementation.
  3. MooTools users are (for whatever reason) associated with having an elitest disposition.

However, you will find ample tools for working with more unique areas of scripting, like JSON file parsing, cookies, and flash embedding to name a few. Also, a convenient download page that lets you choose exactly what you need for your project so you can ensure the smallest file size possible.

Step 1 - Get MooTools!

Head over to the MooTools Core Builder page! For this particular project, you'll want to select Fx.Morph, Element.Event, DomReady, and Selectors and hit "Download" using YUI Compressor. All dependencies are automatically chosen for you. Be mindful, as certain browsers will add a '.txt' extension to a javascript file for your protection. This will obviously need to be removed, and feel free to trim off some of the beefy characters in the file name.

Step 2 - Attach MooTools to your HTML document

Create the HTML document you'll be using for this project, and attach the MooTools library. My page head looks something like this:

1
2
3
	<head>
4
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
	<title>Mootools - Product Highlights!</title>
6
	
7
	<script src="mootools.js" type="text/javascript"></script>
8
	
9
	...
10
	
11
	</head>

Step 3 - Lay down your CSS and HTML

Take a look at the following styles and HTML to see how I've laid out the page.

CSS code:

1
2
3
<style type="text/css" media="screen">
4
	body {
5
		margin: 0;
6
		padding: 0;
7
		background: #1a1613 url(images/bg_tutBody.gif) repeat-x;
8
	}
9
	img { border: 0; }
10
	
11
	#siteWrap { /* The wrapper for my page icons and bubbles */
12
		margin: 287px auto 0 auto;
13
		width: 642px;
14
		height: 345px;
15
		position: relative;
16
		background: transparent url(images/bg_pageWrap.jpg) no-repeat top left;
17
	}
18
	
19
	#pageWrap { /* Wrapper for my page icons */
20
		position: absolute;
21
		z-index: 5;
22
		top: 138px;
23
		left: 134px;
24
25
	}
26
	/* Page specific styles */
27
	#psdPage {
28
		margin-right: 19px;
29
		float: left;
30
		cursor: pointer;
31
	}
32
	#netPage {
33
		margin-right: 20px;
34
		float: left;
35
		cursor: pointer;
36
	}	
37
	#audioPage {
38
		float: left;
39
		cursor: pointer;
40
	}
41
	#bubbleWrap { /* Wrapper for my bubbles */
42
		position: absolute;
43
		z-index: 10;
44
		left: 158px;
45
		top: 30px;
46
	}
47
	.bubble {
48
		position: absolute;
49
	}
50
51
</style>

HTML code:

1
2
3
<div id="siteWrap">
4
	<div id="bubbleWrap" style="visibility: hidden;">
5
		<div class="bubble"><img src="images/bubble_PSD.jpg" alt="PSDTUTS" /></div>
6
		<div class="bubble"><img src="images/bubble_NET.jpg" alt="NETTUTS" /></div>
7
		<div class="bubble"><img src="images/bubble_AUDIO.jpg" alt="AUDIOTUTS" /></div>
8
	</div>
9
	
10
	<div id="pageWrap">
11
		<div class="page" id="psdPage"><a href="#"><img src="images/page_PSD.jpg" alt="PSDTUTS" /></a></div>
12
		<div class="page" id="netPage"><a href="#"><img src="images/page_NET.jpg" alt="NETTUTS" /></a></div>
13
		<div class="page" id="audioPage"><a href="#"><img src="images/page_AUDIO.jpg" alt="AUDIOTUTS" /></a></div>
14
		
15
	</div>
16
</div>

Notice how I have the HTML laid out. I will not be using ID's to select any of the page or bubble elements, and instead creating arrays of all elements containing the two classes, which will allow for this script to scale regardless of how many items you choose to highlight! All bubbles and pages are contained in a wrapper which is absolutely positioned within the site wrapper (which contains our background where all of this is sitting on top of).

Step 4 - Add your javascript

We'll start by creating a wrapper function for our javascript code which places an event listener on the window object, firing once the DOM is loaded and ready. This is important because we need our script to immediately begin altering the DOM once its available.

If we DON'T use this wrapper function, it's quite likely you'll get errors claiming that certain elements don't exist. Another option could be to place the embedded javascript at the end of the document body. However, if you decide to attach the script externally, you'll run into this problem once again, so it's a good habit to get into now!

Another option for 'domready' is to use 'load' which will fire once the page (images included) is completely loaded. We don't want this for this particular project because it means images (such as our bubbles) might intitally flash on the screen before being hidden by our script.

One other important thing to note—if you DO decide to link this script from an external '.js' file, you'll want to ensure you link it AFTER you've linked the MooTools library in the document head.

1
2
window.addEvent('domready', function() {
3
4
	...
5
6
});

Next we start by creating the arrays for both our page and bubble elements and set some initial in-line styles.

1
	
2
3
window.addEvent('domready', function() {
4
	
5
	// Create variables (in this case two arrays) representing our bubbles and pages

6
	var myPages = $$('.page');
7
	var myBubbles = $$('.bubble');
8
	
9
	// Set bubbles opacity to zero so they're hidden initially 

10
	// and toggle visibility on for their container	back on

11
	myBubbles.setStyle('opacity', 0);
12
	$('bubbleWrap').setStyle('visibility','visible')
13
	
14
});

Lastly, we'll attach event listeners to the page icons to fire morph effects on their corresponding bubbles. Note that the order of the bubbles as laid out in the HTML is the SAME order of the page icons. This is important!

1
2
window.addEvent('domready', function() {
3
	
4
	// Create variables (in this case two arrays) representing our bubbles and pages

5
	var myPages = $$('.page');
6
	var myBubbles = $$('.bubble');
7
	
8
	// Set bubbles opacity to zero so they're hidden initially 

9
	// and toggle visibility on for their container	back on

10
	myBubbles.setStyle('opacity', 0);
11
	$('bubbleWrap').setStyle('visibility','visible')
12
	
13
	// Add our events to the pages

14
	myPages.each(function(el, i) {
15
		/* Here we change the default 'link' property to 'cancel' for our morph effects, which 

16
		   ensures effects are interrupted when the mouse is leaving and entering

17
		   to immediately begin the morph effect being called */
18
		el.set('morph', {link : 'cancel'});
19
		
20
		el.addEvents({
21
			'mouseenter': function() {
22
				myBubbles[i].morph({
23
					'opacity' : 1,
24
					'margin-top' : '-15px'
25
				});
26
			},
27
			'mouseleave' : function() {
28
				myBubbles[i].morph({
29
					'opacity' : 0,
30
					'margin-top' : 0
31
				});
32
			}	
33
		});
34
	});
35
});

You'll notice that we're attaching a function using the each() method to all of the elements of the 'myPages' array. And for each function we pass in 'el' which represents the page element, and 'i' which is an integer representing the placement of that page element within its array. We use the 'i' variable for calling the morph effect on the appropriate and corresponding bubble element within the 'myBubbles' array.

And thats it! Pretty painless wasn't it? Be sure to view the working demo, and also bookmark the MooTools Docs page and MooTools Download page for future reference! I hope this tutorial was helpful, and I look forward to putting together something a bit more advanced in the near future using the power of MooTools classes!

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.