7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. Web Development

Create a Simple, Powerful Product Highlighter with MooTools

Scroll to top
Read Time: 7 mins

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:

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:

HTML code:

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.

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

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!

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!

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.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.