Home > Web Front-end > JS Tutorial > How to simply use YUI to create JavaScript animation_YUI.Ext related

How to simply use YUI to create JavaScript animation_YUI.Ext related

WBOY
Release: 2016-05-16 19:16:48
Original
1062 people have browsed it

原文地址: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);
Copy after login
产生一个从上移动下来的效果:
exdiv.moveIn('top');
Copy after login
播放动画:
exdiv.play();
Copy after login

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

更多复杂的例子
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
<font face="Arial">var animator = new YAHOO.ext.Animator();<br>var cursor = new YAHOO.ext.Actor('cursor-img', animator);  <br></font><font face="Arial">var resize = new YAHOO.ext.Actor('resize-img', animator); <br>var click = new YAHOO.ext.Actor('click-img', animator);  <br>var splitter = new YAHOO.ext.Actor('splitter', animator);   <br>animator.startCapture();  <br>animator.addAsyncCall(Blog.navbar.undockDelegate, 1);  <br>cursor.show();  <br>cursor.moveTo(500,400);  <br>cursor.moveTo(20, getEl('navbar').getY()+10, true, .75); <br>click.clearOpacity(); <br>click.show();  <br>click.alignTo(cursor, 'tl', [-4, -4]); <br>animator.pause(.5);  <br>click.hide(true, .7);    <br>animator.addAsyncCall(Blog.navbar.dockDelegate, 1);    <br>cursor.alignTo('splitter', 'tr', [0, +100], true, 1); <br>resize.alignTo('splitter', 'tr', [-12, +100]);<br>animator.beginSync(); <br>cursor.hide();  <br>resize.show();  <br>animator.endSync();  <br>splitter.highlight('#EEEEFF', '#C3DAF9', .3); <br>splitter.highlight('#EEEEFF', '#C3DAF9', .3); <br>animator.pause(2);<br>resize.hide(); <br>cursor.show(); <br>cursor.moveTo(-100, -100, true);   <br>animator.stopCapture();  <br>animator.play();</font>
Copy after login
如果你喜欢做动画,那你一定会爱上yui,特别是现在做动画更容易了。
Note: These functions are also included in YAHOO.ext.UpdateManager. This is a simple API that uses YAHOO.util.Connect to AJAX update elements. It is event-driven to make YAHOO.util.Connect code more concise. The best thing is that you go and take a look at the internal code yourself because I am too tired to write BLOGL anymore!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template