Fork me on GitHub

30 days of
MooTools


30 days of MooTools is a collection of tutorials to learn to use the MooTools javascrit library, first published in 2008 by ConsiderOpen.

The lessons are actually 23, as they where interrupted after this day.

As the original site is currently offline, the tutorials have been collected using the Wayback Machine to make them available to everyone interested in learning to use MooTools.

The tutorials are based on MooTools 1.2 so some parts could be deprecated and they should be updated and expanded based on the new MooTools versions.

The copyright (and gratitude) for the original lessons is hold by ConsiderOpen.




Idea Paolo Manganiello. Licence CC 3.0.

Intro to the Mootools 1.2 Library

We recently got a request to do 30 days of Mootools 1.2 tutorials and it seemed like such a fine idea that we decided to get started right away. These tutorials will assume no previous Mootools or JavaScript experience, but will require basic knowledge of html and CSS.

Introduction to Mootools 1.2 JavaScript Library

Mootools 1.2 is a powerful, lightweight javascript library designed to ease interactive JavaScript web development. In a way, you can think of a lot of things that Mootools can do as CSS extensions. For example, CSS lets you change a property on hover. Javascript allows you to recognize more events (click, mouseover, keystrokes, …) and Mootools makes it almost painfully easy to take advantage of this.

In addition, Mootools has all sorts of nifty extensions which let not only change element properties (like you can with hover), but lets you “morph” or “tween” properties, giving you the ability to create animated effects like you see on our menu.

Thats just one example, Mootools can allow you to do much more. Over the next 30 days, we are going to dig deeper into the Mootools library, exploring everything from arrays and functions to FX.Slide and the other bundled plugins.

Installing Mootools 1.2

First, download and install the Mootools 1.2 Core library.

  1. Download the Mootools 1.2 Core library
  2. Upload the Mootools 1.2 Core library to your server or workspace
  3. Link to the Mootools 1.2 Core library in your head tag <script src=”mootools1.2core.js” type=”text/javascript”>

Add Script Tags in your Head Tag

Now that you are calling Mootools into your page, you need a place to write the code. There are two options:

1. Place the code between script tags in your head:

<script type="text/javascript"><!--mce:0--></script>

2. Create an external JavaScript file and link to it in your head:

<script src="myJavaScriptFile.js" type="text/javascript"><!--mce:1--></script>

From here on out, you can use either method. I will often call the functions within the domready event inside the script tags, but create my functions in an external JavaScript file.

Put it in the domready

Mootools functions must be called within the domready event.

window.addEvent('domready', function() {
    exampleFunction();
});

Put it in a Function

You can still build your function outside of the domready, then call it within.

var exampleFunction = function() {
     alert('hello')
};

window.addEvent('domready', function() {
    exampleFunction();
});

Library Description

For this first tutorial, we are going to take a closer look at the key components of the library’s architecture and go over some of the basic functionality.

Core

The core contains common functions within the Mootools library and makes it easy to accomplish common tasks as well as beefing up a lot of pre-existing functionality (more on that later). The following is meant only as an example of some of Mootools’ capabilities and is no replacement for reading the Mootools documentation.

Native

This section of the library also contains common tools, letting you manipulate arrays (basically a list of values or objects), functions, numbers, strings, hashes and events. Here are a few of the tools featured in Native:

  • Create a script that will apply to each object within an array - .each();
  • Get the last item within an array - .getLast();
  • Create an event that happens every x milliseconds - .periodical();
  • Round a decimal - .round();
  • Convert rbg to HEX - .rgbToHex();

Class

A JavaScript class (in contrast to a CSS class), is a reusable object with functionality. To learn more about Mootools classes you can check out this quick intro by Valerio (Mootools Classes - How to Use Them). I would also recommend David Walsh’s Mootools Class Template.

Element

The element classes provide some of the most useful functionality within the Mootools library. Here is where you will select Dom elements, manipulate their properties and position, and change their CSS styles. Here are few of the great tools Mootools provides to deal with Dom elements:

  • Select the first of a certain type of DOM element, ID or class - .getElement();
  • Select all of a certain type of DOM element, ID or class - .getElements();
  • Add a class to an element - .addClass();
  • Find out the value of an element’s property - .getProperty();
  • Change the value of an element’s property - .setProperty();
  • Find out the value of an element’s style property - .getStyle();
  • Change the value of an element’s style property - .setStyle();
  • Get an elements coordinates - .getCoordinates();

Utilities

Utilities provides more refined selector logic, includes the domready event, gives you tools to manage AJAX calls, lets you easily manage cookies and even includes the “swiff” functionality which lets you interface JavaScript with ActionScript.

FX

This is probably Mootools’ most fun section. With FX you can create “tween” and “morph” effects that add animation to your DOM objects.

  • Create an animated transition between two style values (e.g. grow a div larger smoothly) - var myFx = new Fx.Tween(element);
  • Create an animated transition between multiple different style values (e.g. grow a div larger smoothly and change the background color while making the border thicker) - var myFx = new Fx.Morph(element);

Request

Contains tools to ease dealing with Javascripts XMLHttpRequest (Ajax) functionality. Above making the entire request/response process much less painful, Request has extensions to deal specifically with either HTML or Javascript Object Notation (JSON) responses.

Plugins

The Mootools plugins extend the core functionality, letting you easily add advanced UI functionality to your web projects. The list of plugins includes:

  • Fx.Slide
  • Fx.Scroll
  • Fx.Elements
  • Drag
  • Drag.Move
  • Color
  • Group
  • Hash.Cookie
  • Sortables
  • Tips
  • SmoothScroll
  • Slider
  • Scroller
  • Assets
  • Accordion

The Big Picture

Before the next tutorial, take some time to pour over the Mootools documentation. Even if you don’t understand it all, just read through it and absorb what you can. Over the next 29 days we are going to dig deeper into specific areas within the library and break Mootools down into some easy to digest pieces, but first, be sure and take a good look over the whole menu.

To Learn More…

A zip with everything you need to get started

Includes a copy of the Mootools 1.2 core library, a simple html file, an external JavaScript file for your functions and a css style sheet. The html is well commented and includes the domready event.

Other Mootools Tutorials

In the mean time, here is a list of some other Mootorials to help you get started.

30 Days of Mootools Translations

Hablas Español? Echa un vistazo a la traducción de alainalemany.com

French translation: http://www.6ma.fr/tuto/mootools+jours+jour+1+introduction-458

Chinese translation: http://ooboy.net/blog/article/605.aspx

Mootools 1.2 Cheat Sheet

Here is a great summary of the capabilities of Mootools 1.2. I just printed out myself a copy and am currently looking for a place to hang it. Maybe i’ll drop by the printers and have them set me up with a poster size :). Anyhow, here’s the link to the Mootools 1.2 Cheat Sheet

Mootools Forum

If you want to chat with other folks about Mootools, check out code samples or dig into specific questions, this is the place for you. It’s still very new, but picking up steam: Mootools 1.2 forum

Tomorrow

Visit our Day 2 tutorial to learn more about Mootools 1.2 selectors