如何简单地用YUI做JavaScript动画


原文地址:http://www.jackslocum.com/blog/2006/08/24/javascript-animations-with-yahoo-ui-made-easy/

YUI的动画类简直就是一门艺术工作。不像其它的传统的JS库,提供了已经“预设好”的直接可运行的效果,相反,它由开发者做自己喜欢的。在这点,我比较喜欢适当地运行一些动画和变换效果,越多越好。

按照这么地说,也会有个问题。动画API是非常“底层”的工作,而且需要您花时间去弄的,子类的构建函数会又长又啰嗦的。因此,在上一发布的版本中, 我把 YAHOO.ext.Element的动画效果尽量简单地调用。其实,在这个网站的大多数效果都是它完成的。

但如果我想做些较复杂的效果,该怎么办?当某个效果完成后,YUI能够以函数的方式提供一个回调(callback).利用Callback你能够将多个效果排队来做出复杂的效果。唯一的问题是,在其函数内,每一步效果的封装好的,这样,代码起来就很复杂可怕了。另外一个问题是,当你同一时内多个元素发生动画效果的话,代码会持续地随着每个元素它拥有的回调函数的增长而增长。不得不说,YahooUI!在这方面,有点难以适用于开发,--尤其日渐常用这类效果。

新版的yui.ext包含了原本yui做动画所需的复杂代码,甚至比你想的要简单。这里是功能列表:

*自动调整动画顺序 --回调函数不再有啦!

* 处理多个元素的动画更方便,--只要设置一下属性。
* 对多个元素的动画效果,自动同步和调整顺序 --只要添加 Actors到一个 Animator 就可以同步。
* 一些常用的预设效果(尽管yui不会引起内存泄漏,但切勿替换、复制script.aculo.us那炫目的效果【 译者frank注:这里是反语,讽刺script.aculo.us会内存泄漏)】
* 允许动画过程中执行任何的函数。
*允许动画过程中调用自动调整的同步函数。

*动画列表(一组的动画效果)可按需预定义和执行

好,让我们看看进入代码部分
以id为example的div新建一个actor对象。第三个参数真告诉它立即开始捕捉动作(否则的话你必须调用startCature()) 如果是false则是一个标准的元素对象,同时执行所有调用。
var exdiv = new YAHOO.ext.Actor('example', null, true);
产生一个从上移动下来的效果:
exdiv.moveIn('top');
播放动画:
exdiv.play();

另外一个范例:
这是范例的目的是在导航上(Jack's Blog)做交换效果
注意: Animators 可以支持一个或以上的元素的排序和同步动画
创建一个animator,包含两个Actor (this.minbar and this.dockbar),然后开始捕捉他们的动作。
var anim = new YAHOO.ext.Animator(this.minbar, this.dockbar); 
anim.startCapture();
开始动画:
this.dockbar.setX(-this.dockedWidth); 
this.dockbar.setWidth(this.dockedWidth);
this.dockbar.setVisible(true);
beginSync() 告诉 animator 组合每个actions的动作,同时播放这些动画。
anim.beginSync(); 
this.minbar.move('left', this.minbar.getWidth()+this.margin, true, .35);
this.dockbar.move('right', this.dockedWidth+this.margin, true, .4, null, YAHOO.util.Easing.easeOut);
anim.endSync();
播放完成后执行这个 callback.
anim.play(this.fireDocked);

更多复杂的例子
Click here to see what it does. Sorry, this depended on an old blog layout and no longer works. I won't go step by step but notice how the code flows like a normal javascript app? You would never know that there are over 10 different asynchronous animations being sequenced. Notice the async calls too - those are calling out to my navbar and telling it to dock or undock, which also performs more animations. The Animator here waits for those animations to complete before continuing. Dont' mind my code spacing, I am big fan of spacing code into logical blocks!2
var animator = new YAHOO.ext.Animator();
var cursor = new YAHOO.ext.Actor('cursor-img', animator);
var resize = new YAHOO.ext.Actor('resize-img', animator);
var click = new YAHOO.ext.Actor('click-img', animator);
var splitter = new YAHOO.ext.Actor('splitter', animator);
animator.startCapture();
animator.addAsyncCall(Blog.navbar.undockDelegate, 1);
cursor.show();
cursor.moveTo(500,400);
cursor.moveTo(20, getEl('navbar').getY()+10, true, .75);
click.clearOpacity();
click.show();
click.alignTo(cursor, 'tl', [-4, -4]);
animator.pause(.5);
click.hide(true, .7);
animator.addAsyncCall(Blog.navbar.dockDelegate, 1);
cursor.alignTo('splitter', 'tr', [0, +100], true, 1);
resize.alignTo('splitter', 'tr', [-12, +100]);
animator.beginSync();
cursor.hide();
resize.show();
animator.endSync();
splitter.highlight('#EEEEFF', '#C3DAF9', .3);
splitter.highlight('#EEEEFF', '#C3DAF9', .3);
animator.pause(2);
resize.hide();
cursor.show();
cursor.moveTo(-100, -100, true);
animator.stopCapture();
animator.play();
如果你喜欢做动画,那你一定会爱上yui,特别是现在做动画更容易了。
注意: 这些功能同样包含在 YAHOO.ext.UpdateManager里面. 这是一个使用YAHOO.util.Connect 来AJAX更新元素的简单API ,基于事件驱动使得YAHOO.util.Connect 代码更简洁。 最好的是,你亲自去看看内部的代码因为我现在实在太累了-不能再写BLOGL了!

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3