Create a Simple, Powerful Product Highlighter with MooTools

Tutorial Details
  • Difficulty: Beginner
  • Completion Time: 30 Minutes

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:


	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Mootools - Product Highlights!</title>
	
	<script src="mootools.js" type="text/javascript"></script>
	
	...
	
	</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:


<style type="text/css" media="screen">
	body {
		margin: 0;
		padding: 0;
		background: #1a1613 url(images/bg_tutBody.gif) repeat-x;
	}
	img { border: 0; }
	
	#siteWrap { /* The wrapper for my page icons and bubbles */
		margin: 287px auto 0 auto;
		width: 642px;
		height: 345px;
		position: relative;
		background: transparent url(images/bg_pageWrap.jpg) no-repeat top left;
	}
	
	#pageWrap { /* Wrapper for my page icons */
		position: absolute;
		z-index: 5;
		top: 138px;
		left: 134px;

	}
	/* Page specific styles */
	#psdPage {
		margin-right: 19px;
		float: left;
		cursor: pointer;
	}
	#netPage {
		margin-right: 20px;
		float: left;
		cursor: pointer;
	}	
	#audioPage {
		float: left;
		cursor: pointer;
	}
	#bubbleWrap { /* Wrapper for my bubbles */
		position: absolute;
		z-index: 10;
		left: 158px;
		top: 30px;
	}
	.bubble {
		position: absolute;
	}

</style>	

HTML code:


<div id="siteWrap">
	<div id="bubbleWrap" style="visibility: hidden;">
		<div class="bubble"><img src="images/bubble_PSD.jpg" alt="PSDTUTS" /></div>
		<div class="bubble"><img src="images/bubble_NET.jpg" alt="NETTUTS" /></div>
		<div class="bubble"><img src="images/bubble_AUDIO.jpg" alt="AUDIOTUTS" /></div>
	</div>
	
	<div id="pageWrap">
		<div class="page" id="psdPage"><a href="#"><img src="images/page_PSD.jpg" alt="PSDTUTS" /></a></div>
		<div class="page" id="netPage"><a href="#"><img src="images/page_NET.jpg" alt="NETTUTS" /></a></div>
		<div class="page" id="audioPage"><a href="#"><img src="images/page_AUDIO.jpg" alt="AUDIOTUTS" /></a></div>
		
	</div>
</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.

window.addEvent('domready', function() {

	...

});

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

	

window.addEvent('domready', function() {
	
	// Create variables (in this case two arrays) representing our bubbles and pages
	var myPages = $$('.page');
	var myBubbles = $$('.bubble');
	
	// Set bubbles opacity to zero so they're hidden initially 
	// and toggle visibility on for their container	back on
	myBubbles.setStyle('opacity', 0);
	$('bubbleWrap').setStyle('visibility','visible')
	
});

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!

window.addEvent('domready', function() {
	
	// Create variables (in this case two arrays) representing our bubbles and pages
	var myPages = $$('.page');
	var myBubbles = $$('.bubble');
	
	// Set bubbles opacity to zero so they're hidden initially 
	// and toggle visibility on for their container	back on
	myBubbles.setStyle('opacity', 0);
	$('bubbleWrap').setStyle('visibility','visible')
	
	// Add our events to the pages
	myPages.each(function(el, i) {
		/* Here we change the default 'link' property to 'cancel' for our morph effects, which 
		   ensures effects are interrupted when the mouse is leaving and entering
		   to immediately begin the morph effect being called */
		el.set('morph', {link : 'cancel'});
		
		el.addEvents({
			'mouseenter': function() {
				myBubbles[i].morph({
					'opacity' : 1,
					'margin-top' : '-15px'
				});
			},
			'mouseleave' : function() {
				myBubbles[i].morph({
					'opacity' : 0,
					'margin-top' : 0
				});
			}	
		});
	});
});

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!

Tags: MooTools
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.csscount.com Mark Abucayon

    That was cool, I like the effect of this one. thanks for sharing this. cool

  • http://laminbarrow.com Lamin Barrow

    WOW. neat effect. Thanks for for the article and for using mootools. :)

  • Dave

    Very nice and clean! it’s possible to share or send me the PSD or source file of graphic part … ? thx

  • Timmy

    What it need to change if I want feature which enable mouse over highlight-image and image won’t disappear if cursor is in navigation or highlight area?

  • http://reazulk.wordpress.com Rubel

    Cool… Effect

  • http://www.namelezz.net Namelezz!

    Thanks for sharing this!

  • Daniel Barreto

    Hey! thanks for the article. And you are right, Mootools Rules!

  • Daniel

    great tutorial! very useful for the project i work on. thanks a lot.

  • Pingback: Best of July 2008 | Life as a Web Designer

  • Jo

    always wondered how that coda effect worked.
    thanks alot

    one question though:
    your html code seems to be suffering from div-itis.
    can’t you just use a ul ?
    or is it needed for the javascript to work?

    compleet javascript noob here

  • Devin

    I’ve tried this script on a brand new design and it works great for me in Internet Explorer and Firefox but other people that try to view it in Firefox cannot see it. Must be an error on my part…not sure

    But this is an amazing effect!

  • http://www.loadmemory.com/blog loadmemory

    Great work! Thanks for sharing!

  • madsheep

    Preaty cool efect.
    Such a shame it takes two times more code with mootols than with jQuery.

  • http://www.codingbanter.com Raj

    cool effect. Thanx a lot!

  • questionare

    I downloaded the source and made a few edits regarding photos, text etc just to play around. But Now it only shows a blank page in internet explorer/Opera and i don´t understand why. I made no big changes but it wont show up in IE. works fine in FireFox

  • Pingback: Web geliştiriciler için harika paketler

  • http://michaelkowens.com MK Owens

    I’ve got to admit; with all of the jQuery content on NETTUTS, I was starting to wonder if I was ever going to find an “immediately useful” tutorial related to Javascript because I’ve been a Mootools user since v1.07 and much prefer its extensible nature to any of the other “big name JS frameworks.” While a lot of the JS on NETTUTS has been useful in giving me inspiration to develop my own scripts (just as valuable as an “immediately useful” tutorial, imho), I would love to see more Mootools-based tutorials, as our community needs the exposure. Most of the companies that I have worked with have loved Mootools once they were given the opportunity to play with it, but because things like this are far and few in between for the Mootools community (as compared to jQuery, Script.aculo.us/Prototype, or even non-framework based JS) it has been a tough battle at times to convince clients and employers to use it on their sites.

    I’m a lot more inclined to write up a few interface tutorials to submit now that I know that some of the people on NETTUTS support Mootools, and Mootools users are allowed to enjoy recess, too.

    As an aside: For all of the people who have mentioned the “elitist attitude” of Mootools users, we’re not all like that. We just don’t get asked to come out and play as often as you jQuery and S/P users. People usually refer to our love of milk as the reason, but that’s okay . . . we’re mad for milk!

  • http://www.detacheddesigns.com Jeffrey Way

    @MK – It’s all a matter of what we receive. We simply don’t get many Mootools submissions. Please feel free to submit a tutorial if you write one up!

  • http://michaelkowens.com MK Owens

    Fair enough, Jeffrey. I’m curious what would be the most requested tutorial submissions (esp. related to Mootools, PHP/MySQL, and gen XHTML/CSS), as I don’t want to submit something that people aren’t all that interested in.

  • http://www.aninterestingfact.com İsmail Kaya

    very useful thank you

  • http://www.piksite.com Pi[ks]ite

    It is nice ! but…
    But if you also use the MooTools More Builder, you will notice a little bug : sometimes the mouseleave event bugs.
    Thus, if you use the More Builder, better is to use Fx.Elements.
    Here is the javascript part to use :

    var Bubbles = new Class({

    options: {
    onOpen: false,
    onClose: $empty,
    transition: Fx.Transitions.linear,
    duration: 500,
    open: null
    },

    initialize: function(elements,bubbles,options){

    this.setOptions(options);
    this.elements = $$(elements);
    this.bubbles = $$(bubbles);

    this.BubbleFx = new Fx.Elements(this.bubbles, {wait: false, duration: this.options.duration, transition: this.options.transition});

    this.elements.each(function(elt,i){

    elt.addEvent(‘mouseenter’, function(e){
    new Event(e).stop();
    this.reset(i);
    }.bind(this));

    elt.addEvent(‘mouseleave’, function(e){
    new Event(e).stop();
    this.reset(this.options.open);
    }.bind(this));

    var BubbleObj = this;

    }.bind(this));

    },

    reset: function(num){

    var BubbleObj = {};

    this.elements.each(function(elt,i){
    BubbleObj[i] = {‘opacity’: 0, ‘margin-top’ : ‘-35px’};
    }.bind(this));

    if($type(num) == ‘number’){
    BubbleObj[num] = {‘opacity’: 1, ‘margin-top’ : ‘-85px’};
    }

    this.BubbleFx.start(BubbleObj);

    }

    });
    Bubbles.implement(new Options, new Events);

    window.addEvent(‘domready’, function() {

    // Create variables (in this case two arrays) representing our bubbles and pages
    var myPages = $$(‘.page’);
    var myBubbles = $$(‘.bubble’);

    // Set bubbles opacity to zero so they’re hidden initially and toggle visibility on for their container
    myBubbles.setStyle(‘opacity’, 0);
    $(‘bubbleWrap’).setStyle(‘visibility’,'visible’);

    new Bubbles(myPages,myBubbles);

    });

  • http://www.piksite.com Pi[ks]ite

    [this post is to edit my previous post in order to respect values from the original code]

    sorry, if you cloud change :

    this.elements.each(function(elt,i){
    BubbleObj[i] = {’opacity’: 0, ‘margin-top’ : ‘-35px’};
    }.bind(this));

    if($type(num) == ‘number’){
    BubbleObj[num] = {’opacity’: 1, ‘margin-top’ : ‘-85px’};
    }

    by :

    this.elements.each(function(elt,i){
    BubbleObj[i] = {’opacity’: 0, ‘margin-top’ : 0};
    }.bind(this));

    if($type(num) == ‘number’){
    BubbleObj[num] = {’opacity’: 1, ‘margin-top’ : ‘-15px’};
    }

  • http://www.omrcm.com omrcm

    I’m enterested avery time on this tecnology.

  • http://www.windows7themes.com Windows Themes

    Wow. I really like this one. Is amazing. Thank you so much

  • http://cajebo.com Michael

    @TS: “(I’m a designer who has to do his own production work for the most part.)

    Thank goodness. Another lost soul.
    :)

    Glad to know there’s another person out there enamored with the power of the matrix, but schooled in design. Stuff like this makes you want to eat the (—–) pill.

    thanks for a great Tute Jeremy.

  • http://fantacalcioanzio.it/index2008.asp Giulio

    Thanx for the tutorial. This effect is very nice and I’m using it on a new site I’m designing in these days.

    Just a little question, If I use a png with trasparent background for the bubbles, the script works fine on fireworks (3.0), while in IE 7 the bubbles, when appear, has a black shadow, than disappear when the image is loaded.

    Think it’s a IE problem, but if anybody know if is it possible to fix it, I appreciate that.

    Sorry for my bad english.

    Thanx in advance

  • Pingback: 75 técnicas (realmente) útiles con JavaScript - Carrero Bitácora de los Hermanos Carrero, David Carrero Fernández-Baillo y Jaime Carrero Fernández-Baillo.

  • Pingback: 75 (Really) Useful JavaScript Techniques | Neurosoftware web dev

  • gRay

    Dude, I’ve been looking for hours on how to apply an effect to an array of elements. MooTools docs weren’t cutting it for me, though it’s probably in there somewhere. Delirious from the lack of sleep, I finally came across this tutorial. Thanks! Me sleep now…

  • Manao

    Very good resources for mootools are also
    available at cnet (which uses mootools very
    much and even made some nice libraries/plugins)

    http://clientside.cnet.com/wiki/
    http://www.mootorial.com/

    For some copy-paste stuff you could also look at moo.rd
    http://www.moord.it/examples/

    just to get you started…

  • http://cjlm.org Love CJ

    Cool Design.Tks

  • http://www.impulsum.com.au Edwin

    Great!!! Very useful for one of my projects

    Thank you

    Greetings from Australia

  • Pingback: Web Resources » Loon Design

  • Pingback: Tooltips Scripts: 50+ Scripts With AJAX, Javascripts, CSS & Tutorials | Web Burning Blog

  • http://yustian.web.id Yustian

    Wooow very beautiful. I wanna using this tool for my blog :)

  • http://esquareda.com Eric E. Anderson

    Well done Tut! Quite inspiring… this is a great option to consider for an eCommere store…

  • Pingback: Tooltips Scripts: 50+ Scripts With AJAX, Javascripts, CSS & Tutorials | Fusuy.com || Webmaster Accessary Platform

  • Drama

    Man I created such a slick effect for my twitter-feed based on this, and then realized that I needed a jQuery-script for something else om the site. When the jQuery-function was built in, it broke the mootools-one… :(

    Does anyone know if there is a jQuery-version of this that I could use instead??

  • http://m1chu.eu/ m1chu

    If somebody (f.e. Drama) want to use this effect with jQuery or Prototype with Scriptaculous, there is a solution (parts of codes and polish interpretation of this jQuery and Prototype codes):

    http://m1chu.eu/2009/02/09/efekt-graficznego-podswietlenia-w-jquery-i-prototype-z-scriptaculous/

  • http://www.tim-christmann.de Tim

    Hehe, I´m a mootools user since 2 years now, and I really like it.

    This effect is nice, think I´ll use it in the next version of my site.
    Note: You should implement a simple css fallback solution in the demo X-)

    Greetz from Germany

  • Mark

    Can this example be used in ASP.NET?

  • Pingback: 20 Excellent Mootools Techniques for Rich User Interface | DevSnippets

  • Mark R.

    Any Idea how to make the first item be visible when the page loads?

  • http://www.cdpradeep.com/ Pradeep CD

    Awesome tut…thanks

  • http://tuts.cgbaran.com CgBaran Tuts

    Excellent tutorial

  • Pingback: 8 Layout Solutions To Improve Your Designs | How-To | Smashing Magazine

  • http://www.briannwebdesign.com BriAnn

    Hi All.

    Another great tut from you guys! I set it up with my ‘bubbles’ below my page elements. I’m using the effect to transition an image below my navigation bar. Had problems to get it to work initially, as I’m no javascript wizard. But eventually got it to work and it’s awesome! Thanks so much for another great tutorial!

    ~Bri

  • Pingback: 8 Layout Solutions To Improve Your Designs | Search Engine Optimisation

  • Pingback: 8种布局解决方案,改善您的设计 | ⊹⊱⋛⋋ISong榮耀ζ組織™⋋⋛⊱⊹

  • Pingback: 八种布局方案改善你的设计(上) | 互联网的那点事...