30 Days of Mootools 1.2 Tutorials - Day 1 - Intro to the Library

- Troy

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">
//Mootools code goes here
</script>

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

<script src="myJavaScriptFile.js" type="text/javascript"></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

We have also been told to expect a Chinese translation shortly.

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

The schedule (so far)

Here is a look at the subjects we are going to cover in this series:

Tutorial Requests?

The plan is to ramp up slowly, dealing with more complex issues later in the series. We would like to hear from you: what basic areas are you interested in learning about? What kind of projects would you like to see covered in these tutorials? Please feel free to drop us a note with any comments, questions or suggestions. Thanks and hope you find this series helpful.

Bookmark and Share

Comments (32) Trackbacks (20)
  1. Mohsen


    Thanks! Great job. I look forward to seeing all the tutorials
  2. Randy Jensen


    This is perfect! I've been looking for something like this.

    I'd like to see a tutorial with an accordion-like tab dropdowns similar to http://nd.edu/ (in the top right) or http://www.richlandcollege.edu/newsite/ (in the same place).
  3. Troy


    @2, great idea Randy. I'll schedule that one for a bit later in the series.
  4. Erika


    Okay, now THIS is what I'm talkin' about! I've TOTALLY been waiting for this!
  5. drazin


    I like the idea here, but you claim "These tutorials will assume no previous Mootools or JavaScript experience, but will require basic knowledge of html and CSS."

    The first step is very vague. if i had no knowledge of javascript i would not know how to call a function or where to put the domready or any of the other steps either.

    you should be clearer in the steps.
  6. aldrin


    great starting, i been working mootools for my previous projects hopefully althought i'm not ver familiar with everything through out the objects but hopefully i would be glad to look forward for your more coming tutorial soon.
  7. Troy


    @5 - You bring up a good point Drazin. I'll add a quick note about script tags and calling in a js file. Thanks!
  8. Edwin


    Great idea! But now I can't choose between learning Mootools or jQuery first. What are the main differences between them and which one do you think has the recommendation?
  9. Troy


    One great thing about Mootools is the speed:
    http://mootools.net/slickspeed/

    Another thing is the structure - it is very tight and easy to use. I get the sense that Mootools is seen as a bit more intimidating than jQuery, as if its much harder to use, but I have worked with both and enjoy Mootools more.

    But don't get me wrong, jQuery is a great library and there are a lot of plugins, but I choose to work with Mootools due to the strength of the architecture and the stability of the bundled plugins.
  10. Pradeep Mamgain


    It was me Pradeep, from www.aniquito.com who requested for it and you did it..

    Wow man, love you for that.

    Thanks.
  11. Troy


    Pradeep, the muse, thank YOU for the idea. Hope this does you some good. Are there any particular areas you would like to cover?
  12. ghazal


    Great tuts. Consistent and clear.
    Have you considered translation into
    other languages ?
  13. lauren


    @12: That's a great idea. We don't know any other spoken languages, but we've been getting mentions from a few different countries. Anyone interested in translating it into another language?

    Let us know: we'll add a link back to your translation and we'll owe you a dinner when we make it to your corner of the planet. :)
  14. boomstix


    dude .. not sure what the rest of your tuorials are going to be like, but this is a wack way of starting out:

    /* here is an example of a function */
    exampleFunction = function() {
    alert('hello')
    };

    /* this is not correct - not _wrong_, but not correct */
    window.addEvent('domready', function() {
    exampleFunction();
    });

    /* this is correct */
    window.addEvent('domready', exampleFunction);


    and saying that "Mootools functions must be called within the domready event" is, again, not _wrong_, but it not correct either - you should say something like "inline Mootools functions (not classes, which we'll get to later), _should_ be called from the domready event, until you understand Mootools events a bit better."
  15. boomstix


    sorry - just read that, and it sounds a little flamey, but its not intended to be. i laud anyone who bothers to put up tutorials.

    so, to clarify the above point, you could say something like this:

    "
    'What is domready, anyway?' i hear you ask, and why do you say functions should be called here?

    The window object has a 'load' event, which fires when _every asset on the page_ (thats images, css, js, applets, etc) has completed loading. Mootools' 'domready' event fires when the entire DOM tree has loaded (ie all the html), so you have every chance of finding that content here using a call to $('container') or $$('div#container'), so that you can declare the tag where you define your function before the tag itself, knowing full well you won't get a JS error.'
    "

    good luck!
  16. Troy


    @14 & 15, thanks for taking the time to clarify that boomstix. The tutorial is generally set up for people with very little programming xp, and i want to keep it as simple as possible.

    As far as the structure of the domready, we are using an anonymous function as will be easier for experimentation. As we get farther into the series, It would be great if you would continue to lend some high level info. Thanks.
  17. Marilyn


    These tutorials are gonna be great!! Is it possible to do a tutorial on transitions between pages?
  18. Troy


    @17, Thanks Marilyn, what exactly do you mean by transitions between pages? Do you between loading html pages? If so, I don't think I've ever come across a technique for controlling the transition... interesting idea tho, not sure if its even possible. Anyone else have any thoughts on this one?
  19. Marilyn


    Not really loading...maybe it does. Here is an example of what I mean (click working example) but this is done with scriptaculous....wasn't sure if this is possible with Mootools?

    http://piotr.zalewa.info/link_triggered_page_transition_fade_out
  20. Troy


    Ah, i c. Yeah, those are totally possible with Mootools. We are going to be covering tween later this week, and from there it looks like its mostly a matter of placing the proper event listeners. Thanks for the suggestion.
  21. Michello


    Thanks! Very useful tutorial!
  22. mr whizard


    I don't have enough words to thank you(Im going through the dictionary to find some:]).
    I have used mootools 1.0 before, and was having some basic difficulties(inspite of the beautiful docs on mootools.net) as i WAS new to JS.
    I have improve my JS skills now And after looking at the topics you will be covering, i think i will be able to do a lot better.
    Thanks a LOTT once again and keep up the gud work coz its ppl like you the web is a beautiful place. Maybe someday ill be in a position to contribute too.
  23. Ari


    Very nice. I am still mucking around with mooTools v.1.11 and find it has many shortcomings that 1.2 seems to solve. (garbage collection issues being one).

    Thanks for all the hard work on this!
  24. Gert Hough


    Hi there
    This looks like something I may like looking into.

    Just to mention a few things...

    1) Although I know html and how to implement certain basic js, I think a slower instruction (more step by step) tutorial will be very much appreciated. (or even better -How about a video)
    2) Could you not also mention a page for demonstrating a certain effect. This could prove to be a real gem as a squeeze page making everyone see the posibilities and signing up for updates of the blog.
  25. Troy


    @24, We have tried to break it down as much as we know how, but if we skipped anything essential, please let us know. As far as your second comment, what kind of effect are you going for? Do you mean more of a description about each day? Thanks, just not quite sure what you meant.
  26. -Oliver-


    Hi,

    I like your tutorial soo much!! There is a compact but very useful information about all topics in the mootools framework.

    Maybe you can give us a list of your favorite external mootools plugins.

    Thank you very much!!!

    Perhaps, there is a chance to enlarge the tutorial to 40, 50, 60, ... days. (but i dont have to be every day, if you don't have enough time)


    Bye!
  27. Troy


    lol, thanks Oliver. For now, we are going to concentrate on getting through the 30 days :)
  28. christine


    These tutorials are really helpful! Great job and thanks for sharing your knowledge!

    As far as requests for mootools tutorials, I'd love to see something on performance, like how to optimize your scripts, what can slow things down, browser-specific performance issues. In my current project I've noticed a huge performance difference between Firefox(fast) and IE(very slow!) and can't find resources on how to improve IE's speed.
  29. Troy


    @28 - I'd be curious to see what code is causing the performance issues. If you have a link, please drop a note and maybe we can figure out how to optimize it.
  30. christine


    Thanks Troy. It seems that the speed problem I was having was due to the PNG background I was using in each of my accordion togglers.

    Within each of these togglers, I had some drag/drop and sortables effects, which were very slow in IE7. Removing that background fixed the speed, so I guess it was more of a browser problem than a mootools one, in this case.
  31. Troy


    Nice, glad to hear you were able to solve your problem.
  32. fearlex


    Super !!!!! Awesome job !!!!!!


Leave a Reply