erm. you can use local element storage. by default, the .get('morph')
will retrieve the first fx instance for you but since you have multiple / make a new one every time and don't use the .get/.set
, you are better off doing something like this instead:
;(function(){
var img = document.id('img');
$('add').addEvent('click', function() {
// try to get from storage, set empty array as default
var fxs = img.retrieve('fxs', []);
// if empty, make instances, store into array
if (!fxs.length) {
[].push.apply(fxs, [
new Fx.Morph(img, {
duration: 1500,
transition: Fx.Transitions.linear.easeOut
}),
new Fx.Morph(img, {
duration: 750,
transition: Fx.Transitions.easeOutCirc
})
]);
// save it as one off
img.store('fxs', fxs);
}
// reference as array elements
fxs[0].start({
top: 175
});
fxs[1].start({
left: 175
}).chain(function() {
// this === fx instance. DRY
this.options.transition = Fx.Transitions.easeInCirc;
this.options.duration = 764;
this.start({
left: 10
});
});
});
$('rem').addEvent('click', function() {
img.removeProperty('style');
});
}());
much later, completely independent code:
;(function(){ // new scope
$('box').addEvent('click', function(){
var fxs = $('img').retrieve('fxs');
fxs && fxs.invoke('stop');
});
}());
this achieves decoupling of code and reliance on variables and makes it DRY and encapsulated--it also re-uses the same fx instances so it's gonna be faster to run after first start.
here's your thing. http://jsfiddle.net/4vBj9/6/
if you're doing complicated and nested animations that need to be smooth in javascript, consider https://github.com/julianshapiro/velocity - it's BY FAR the fastest way to animate anything, including CSS transitions (check the benchmarks). oh - also can do css3 props, which mootools can't.