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

May 16, 2016 pm 07:16 PM

原文地址: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!
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles