30 Days of Mootools 1.2 Tutorials - Day 10 - Using FX.Tween

- Troy

Tween with Mootools 1.2

If you haven’t already, be sure and check out the rest of the Mootools 1.2 tutorials in our 30 days series.

Welcome to Day 10 of 30 Days of Mootools Tutorials, today we are going to look at Fx.Tween. Just like all the other Mootools functions we have seen, these are very simple to use, but give you a lot power. Tween lets you add those great “flashy” effects - which translates to smooth animated transitions to improve your users’ experience.

Tween Shortcuts

We usually start out with “the basics,” but Mootools 1.2 just provides such fantastic shortcuts for tween and they are so easy to use that I couldn’t resist starting here.

.tween();

A tween is a smooth transitions between two style property values. For example, with tween you can smoothly transition a div from “width: 100px” to “width: 300px.”

//create a new function
var tweenerFunction  = function() {
        //now select the element you want to tween
        //then tack on .tween
        //finally declare the style property and value you want to tween to
	$('tweener').tween('width', '300px');
}
 
window.addEvent('domready', function() {
        //here we add an event to a button to initiate the tween on click
        //and we call our tween function
        $('tween_button').addEvent('click', tweenerFunction);
});

Our html for the above would look something like this:

<div id="tweener"></div>
<button id="tween_button">300 width</button>

.fade();

This function lets you smoothly adjust an elements opacity. The set up can look pretty much like the previous example:

//create a new function
var tweenFadeFifty = function() {
       //here you create your selector
       //then you add .fade
       //finally declare a value between 0 and 1 (0 being invisible, 1 being full visibility)
	$('tweener').fade('.5');
}
 
window.addEvent('domready', function() {
        //here we add an event to a button to initiate the tween on click
        //and we call our tween function
        $('tween_fade_fifty').addEvent('click', tweenFadeFifty);
});
<div id="tweener">
<button id="tween_fade_fifty">Fade to fifty percept opacity</button>

.highlight();

Highlight is a very targeted (and extremely useful) tween shortcut that provides two functions:

  1. Use it to tween flash a different background color
  2. Immediately set a different background color, then tween flash another background color

These are very useful for creating user feedback. For example, you can flash an element when something is selected, or you change a color then flash when it has been saved and closed. There are tons of options and it is very simple to use. For this example, lets create two divs and attach the two types of highlight:

//create a function
var tweenHighlight = function(event) {
        //here we are using event.target, passed from the function
        //this translates to "the target of the event"
        //and since the effect applies to the same element that the event is attached to
        //we don't have to create the selector again
        //Note: addEvent will automatically pass the event object as a parameter
        //to the function it calls... very handy
	event.target.highlight('#eaea16');
}
 
//create a function
//you need to pass a parameter
//since this function is being called in an event
//the function will automatically be passed the event object
//and you can then refer to the element with .target
//(the target of the event)
var tweenHighlightChange = function(item) {
        //here instead of a single color, we add a comma separated second
        //this will set the first color, then transition to the second
        item.target.highlight('#eaea16', '#333333');
}
 
window.addEvent('domready', function() {
	$('tweener').addEvent('mouseover', tweenHighlight);
        $('tweenerChange').addEvent('mouseover', tweenHighlightChange);
});
<div id="tweener"></div>
<div id="tweenerChange"></div>

Shortcut Examples

Here is a live example of some of the effect shortcuts. Try playing with the buttons in different orders and notice the behaivor:

var tweenerFunction  = function() {
	$('tweener').tween('width', '300px');
}
 
var tweenerGoBack  = function() {
	$('tweener').tween('width', '100px');
}
 
//.fade will also accept 'out' and 'in', equaling 0 and 1 respectively
var tweenFadeOut = function() {
	$('tweener').fade('out');
}
 
var tweenFadeFifty = function() {
	$('tweener').fade('.5');
}
 
var tweenFadeIn = function() {
	$('tweener').fade('in');
}
 
var tweenHighlight = function(event) {
	event.target.highlight('#eaea16');
}
 
window.addEvent('domready', function() {
	$('tween_button').addEvent('click', tweenerFunction);
	$('tween_reset').addEvent('click', tweenerGoBack);
	$('tween_fade_out').addEvent('click', tweenFadeOut);
	$('tween_fade_fifty').addEvent('click', tweenFadeFifty);
	$('tween_fade_in').addEvent('click', tweenFadeIn);
	$('tweener').addEvent('mouseover',tweenHighlight);
});
<div id="tweener"></div><br />
<button id="tween_button">300 width</button>
<button id="tween_reset">100 width</button>
<button id="tween_fade_out">Fade Out</button>
<button id="tween_fade_fifty">Fade to 50% opacity</button>
<button id="tween_fade_in">Fade In</button>
#tweener {
	height: 100px
	width: 100px
	background-color: #3399CC
}

Mouseover to see highlight type 1





More on Tweens

Creating a new tween

If you want more flexibility and control over your tween effect, you are going to want to create a new tween instead of using the shortcuts. Notice the use of “new” to create a new instance of Fx.Tween:

window.addEvent('domready', function() {
        //first we pass the element we want to tween to a var
        var newTweenElement = $('newTween');
 
       //now we create a new tween event, and pass that element as a parameter
       var newTween = new Fx.Tween(newTweenElement);
});
<!-- here is the element we want to apply the tween to -->
<div id="newTween"></div>
 
<!-- here is a button that will come into play shortly -->
<button id="netTween_set">Set</div>

Setting styles with tween

Once you create a new tween from an element, you can easily set a style property with .set();. This takes place instanstly and works just like .setStyle();

var newTweenSet = function() {
        //notice the use of "this"
        //read on to learn how "this" is used
	this.set('width', '300px');
}

As we learned before, we want to separate our functions outside of the domready event, but in this case we are going to create the tween inside the domready, then refer to it outside. We have already covered one way to pass parameters outside of the domready (by creating an anonymous function and passing a parameter) and today we are going to learn a Moo-better way to pass the tween object (not to say there are not times an anonymous function isn’t more appropriate).

.bind();

With .bind();, we can make a parameter equal to “this” in the eyes of a given function:

//first we add a click event to the button we saw above
//then, instead of just calling the function
//we call the function and add ".bind()"
//we then place whatever we want to pass to the function
//now, inside the function "newTweenSet," "this" will refer to "newTween"
$('netTween_set').addEvent('click', newTweenSet.bind(newTween));

So if we look at the function above, “this” is now equivalent to saying “newTween” (which is our tween object).

Let’s put the whole thing together:

//here is our function
var newTweenSet = function() {
        //since we used bind, "this" now refers to "newTween"
        //so we are really saying
        //newTween.set('width', '300px')
	this.set('width', '300px');
}
 
window.addEvent('domready', function() {
        //first we place our element into a var
	var newTweenElement = $('newTween');
 
        //then we create a new FX.Tween and assign it to its own var
        var newTween = new Fx.Tween(newTweenElement);
 
        //now we add our event and bind newTween and newTweenSet
	$('netTween_set').addEvent('click', newTweenSet.bind(newTween));  
});

Starting a tween effect

Now that we have our tween object all set up, our function is outside of of the domready event and we learned out how to .set(); a style parameter, lets move onto the actual tweening. Starting a tween is very simple and similarly to .fade();, there are two ways to use .start();

  1. Tween a property value from the existing value to another
  2. Set a property value, then tween to another
//this will tween from the existing value of width to 300px
newTween.start('width', '300px');
 
//this one will first set the width to 100px, then tween to 300px
newTween.start('width', '100px', '300px');

Now, you can .start(); the tween from inside a function and use ‘this’ once you .bind() the function with “newTween.”

Thats all on tweens for now…

Although, there is still a lot to talk about regarding tweening effects. For example, if you want to “tween” multiple style properties at once, you would use .morph();. And you can use the transitions library to change the, well, transition. But this tutorial is already long enough, so we will leave those for another day.

To Learn More…

Download a zip with everything you need to get started

Including the Mootools 1.2 core, the examples above, an external javascript file, a simple html page and a css file.

As always, your best resource is the Mootools 1.2 docs.

Tomorrow’s Tutorial

We will continue looking at the Fx library, focusing on morph, as well as the Fx options and events.

Questions, Comments or Suggestions?

This is going to be kind of a 3 part series (tween, morph and transitions) and it would be great to know what else on this subject you would like to see. And as always, any questions, comments or suggestions are welcome. Hope you have found this series useful.

Bookmark and Share

Comments (12)
  1. Peter


    again, nice work man
  2. Brad


    Great tuts I love it :)

    is it possible that you guys convert the tuts also to PDF or something? so it's easier to print.. :)

    thanks once again.
  3. benjamin


    This just keeps getting better and better!
    This was a fun one today.

    Here are a few more tutorial ideas if possible.
    1. Creating a stylesheet switcher.
    2. Creating those cool slightly transparent tool tips that where once listed in the original mootools site.
    3. using fx.slide to toggle a block of text in and out.

    Cool.
    Thanks again!
  4. Troy


    Thanks everyone.

    @2 - we have been talking about different ways to get these tutorials into a downloadable form. For now, its probably just going to be these entries, but once we get them all out we will look into bundling the whole thing.

    @3 - great suggestions. Until we get to the fx.slide tutorial, check out our plugin, toggle-o-matic. It is an extremely simple use of fx.slide, but if it happens to be what you need, it's pretty useful.

    http://www.consideropen.com/blog/2008/07/toggle-o-matic-for-mootools-12/
  5. Peace


    Great! I'm waiting for your tutorial on drag and drop 'cause I found things went wrong when I was trying to addEvents 'drop' and so on to an element. I hope I can get valuable information from you article.
    Thanks for your works.....
  6. Troy


    @5 - Thanks Peace. The Drag and Drop is scheduled for Day 12, so the day after tomorrow. Hope it helps you out.
  7. Felix


    There is an error:

    At explaining .highlight(); you forgot to add:

    var tweenHighlightChange = function(item) {

    the item as parameter!!! Wont work without!
  8. Troy


    Thanks a lot Felix, really appreciate your QA here :)
  9. Kent Pilkington


    So, just to make sure I understand ('cause I've never seen it clearly explained on the Mootools site), I would generally want to create a separate tween object for each DOM element that I want to tween? If I had multiple objects that should receive the same effect (i.e. four buttons that pulse individually when moused over), would I need to create and assign four different tweens, or would I somehow use the same one for each since the effect is the same, or what? I realize there may be multiple ways to do this, but I would guess that each way has trade-offs. I just don't know what they are!! :-)
  10. Troy


    @9 - One thing you could do is use fx.elements. That lets you apply the same options and events to a set of elements, while still having different tween rules fire on different elements. You could also set up your new tween instance within a function and pass it the parameters, calling it to apply to whatever element you want, keeping the same tween effect/options/events.
  11. Bill Massie


    This is a minor quip, but the comments for the code sample in the .bind() section are a little misleading. My quip is with this part:

    //then, instead of just calling the function
    //we call the function and add ".bind()"

    You aren't CALLING the function, you are PASSING the function. The function doesn't get called until the event occurs.

    I suggest rewording it like this:

    then, instead of just passing the newTweenSet function reference we call newTweenSet.bind(newTween) which returns a new
    function that when executed will call newTweenSet with "this" bound to "newTween"

    I guess this might be a little dense for a code comment. Maybe you could expand on it in the introductory paragraph.

    I should add, that I love the tutorials and appreciate all the effort that you guys are putting into them.
  12. Troy


    Bill, thank you for pointing out this important distinction. As soon as I have a moment, I will be sure to update the tutorial.


Leave a Reply