MooTools 1.2 for The Non Programmer – Part 2
March 9, 2008 — admin
In the last MooTools tutorial we saw how easy it is to add event handlers and use the built in effects. This time we will
take it a bit further and work with some custom effects. Sometimes the built in effects dont always meet the requirements of a particular
site or we want a bit fancier javascipt effect for a particular situation. For these such cases, MooTools makes it super easy to put together
a custom effect.
We are going to start out by using one of the new features in MooTools 1.2. Every element now has it’s own instance of the Tween and Morph classes built in. I’ll explain both of them and we’ll see a few examples along the way. First we will start with tween. Tween allows us to animate just about any CSS property there is: height, width, opacity, color, etc. Let’s start with opacity for a nice little fade effect:
window.addEvent('domready', function() {
$('clickMe1').addEvent('click', function(e) {
$('fadeDiv').tween('opacity', 0);
e.stop();
});
$('clickMe2').addEvent('click', function(e) {
$('fadeDiv').tween('opacity', 1);
e.stop();
});
});
Lets up the ante a bit and see what magic we can do with the morph class. The morph class is similar to the tween class in that it lets us animate
CSS properties of any element. The difference is that morph will allow us to animate as many CSS properties as we want all simultaneously!
Try the demo below to see what we can do. So that you can see exactly what is happening I made this animation go pretty slowly (2 full seconds).
window.addEvent('domready', function() {
$('morphDiv').set('morph', { duration: 2000 });
$('clickMe3').addEvent('click', function(e) {
$('morphDiv').morph({
color: '#000',
backgroundColor: '#fff',
width: 400,
height: 50,
borderWidth: 10,
borderColor: '#ff9900'
});
e.stop();
});
$('clickMe4').addEvent('click', function(e) {
$('morphDiv').morph({
color: '#fff',
backgroundColor: '#000',
width: 300,
height: 200,
borderWidth: 1,
borderColor: '#ddd'
});
e.stop();
});
});
The code above and in the previous tutorial should be enough to really kick start your creative side and make some nice effects to compliment any web site. More to come in the next installment…
But the big question is: why is there even a Tween Class? Why should i ever use it instead of the Morph class? Is it somewhat more memory efficient or something?
The Tween class will be a bit faster than morph class if you are only animating one CSS property. If you are animating more than one property than Morph is your only bet.
I get an error saying that window.addEvent is not a function
Make sure that your script tag for mootools.js is *above* the code where you call window.addEvent and you should be good to go.
@Andy:
Also, check to make sure you’ve got the path to mootols right in the element.
Just wondering, what does e.stop(); do?
@David, e.stop() will stop the default event from happening. It is most often used when handling a click event so that the default anchor href isn’t opened. It gets stopped.
Cheers, that’s most enlightening. I’ve actually been wondering if it was possible to trigger a morph event before forwarding the user to the selected url, and that sounds as though it would be a big part of achieving that. Thanks for your help.