http://dojotoolkit.org/documentation/tutorials/1.10/effects/index.html

In this tutorial, we will explore the effects that Dojo provides, which allow us to jazz up your page or application!

在本教程中,我们会探索dojo提供的特效功能,这可以使我们的Web页面或应用更加生动活泼。

Getting Started 开始

By this point, we're pretty comfortable manipulating the DOM and handling events from DOM nodes. However, as we take some of these actions, the transitions can be very abrupt: removing a node makes it disappear from the page, and that can potentially be disorienting for a user. Using some of the standard effects that Dojo provides, we can create smoother user experiences that really add an additional bit of polish and shine to our applications. Further, if we tap into Dojo's dojo/_base/fx and dojo/fx modules, we can chain and combine these effects to make some really dazzling, dynamic experiences.

我们已经可以很方便的操纵DOM以及DOM节点的事件。但是有些操作先的太突兀:例如我们删除一个节点,该节点会从页面上直接消失掉,这样会让用户感觉到很突兀。然后我们可以使用DOjo提供的一些标准特效来装饰我们的动作,例如在添加一个节点时,可以让添加的节点高亮显示一下。我们调用dojo的dojo/_base/fx和Dojo/fx模块,可以让我们的应用和页面可以让我们的用户有更好的使用体验。

Dojo 1.10 has two fx modules: dojo/_base/fx and dojo/fx.

dojo1.10包含了两个fx模块,dojo/_base/fx and dojo/fx。

  • dojo/_base/fx provides base effects methods that were found previously in Dojo base, including: animateProperty, anim, fadeIn, and fadeOut.
  • dojo/fx provides more advanced effects, including: chain, combine, wipeIn, wipeOut and slideTo

dojo/_base/fx 提供了老版本的dojo也提供的基础效果函数,例如动画、淡淡出等。

dojo/fx 提供了更加高级的特效,例如chain, combine, wipeIn, wipeOut and slideTo等。

Fading 淡入淡出

One animation you might have seen in applications you have used is a node fading in or out. This effect is so common and simple that it's included as a part of the core effects in dojo/_base/fx. We can use it to hide or show elements on a page in a way that feels really smooth and polished. Here's an example:

在你的web应用中,最常见的动画之一就是节点的淡入和淡出效果。该功能是模块dojo/_base/fx的核心部分,使用起来简单方便。我们可以使用该模块使节点进行平滑和高亮的显示或隐藏。例如如下:

 <button id="fadeOutButton">Fade block out</button>
<button id="fadeInButton">Fade block in</button> <div id="fadeTarget" class="red-block">
A red block
</div>
<script>
require(["dojo/_base/fx", "dojo/on", "dojo/dom", "dojo/domReady!"], function(fx, on, dom) {
var fadeOutButton = dom.byId("fadeOutButton"),
fadeInButton = dom.byId("fadeInButton"),
fadeTarget = dom.byId("fadeTarget"); on(fadeOutButton, "click", function(evt){
fx.fadeOut({ node: fadeTarget }).play();
});
on(fadeInButton, "click", function(evt){
fx.fadeIn({ node: fadeTarget }).play();
});
});
</script>

All animation functions take one argument: an object with properties. The most important property you will use is the node property: a DOM node or string ID of a node to animate. Another property is duration, which is how long the animation should last, specified in milliseconds. The duration defaults to 350 milliseconds. There are other properties for other animations, but fading doesn't require them.

所有的动画函数有包含一个属性集合对象。这些属性主要包括以下几个:

node:值为DOM节点对象或标识该节点的ID字符串,我们定义的动画就执行对象就是该节点。

duration:该属性定义了本动画执行的时间,单位是毫秒。该属性的默认值是350毫秒。

还有一些其他的而实行,但fading效果不需要他们,在此我们就不列出了。

Animation functions return a dojo/_base/fx::Animation object with several methods: play, pause, stop, status, and gotoPercent. Animations are not started when they are created and must be manually started with the play method, as shown above.

动画函数执行后返回dojo/_base/fx::Animation对象,该对象包含了play、pause、stop、status和gotoPercent等函数。动画被创建之后,并不会立即执行,就像上面我们写的代码一样,必须调用play函数,动画才会执行。

View Demo

Wiping 擦拭

Another effect you might have seen is wiping: changing the height of a node while leaving the content alone. This makes it look like someone is using a windshield wiper on the node. Often, wiping could be a useful effect to create something like a pulldown on a page, where you might have some sort of infrequently accessed content or settings, or perhaps you just prefer the shrinking to fading.

另外一个我们常见的效果就是wiping:不管节点里面的内容,改变节点的高度。这样看起来就想一个雨刷,把节点给擦除掉。这种效果在页面设计下拉的时候会经常用到,例如有些内容不是太重要,可以先隐藏不显示,想要查看的时候,可以通过点击执行wiping效果,展示出隐藏的内容。

下面是wiping的示例:

 <button id="wipeOutButton">Wipe block out</button>
<button id="wipeInButton">Wipe block in</button> <div id="wipeTarget" class="red-block wipe">
A red block
</div>
<script>
require(["dojo/fx", "dojo/on", "dojo/dom", "dojo/domReady!"], function(fx, on, dom) {
var wipeOutButton = dom.byId("wipeOutButton"),
wipeInButton = dom.byId("wipeInButton"),
wipeTarget = dom.byId("wipeTarget"); on(wipeOutButton, "click", function(evt){
fx.wipeOut({ node: wipeTarget }).play();
});
on(wipeInButton, "click", function(evt){
fx.wipeIn({ node: wipeTarget }).play();
});
});
</script>

View Demo

The wipe effect is in the dojo/fx module. In this example, we have added the "wipe" class to our target node. Because the wipe functions operate on the height of the contents of a node rather than a definite height, our "wipe" class sets the target node's height to "auto".

wipe效果定义在dojo/fx模块中,在上面的例子中,我们就在目标节点上添加了wipe效果代码。因为wipe函数是操作节点内容的高度,而不是制定的高度,所以我们的wipeclass设置目标节点的高度为“auto”。

Sliding滑动

So far we've covered hiding nodes, but what if we're looking to move them around a bit? A fade or a wipe doesn't change the node's location. That's where fx.slideTo comes into play. Shifting a node around could be useful to create an appearance of movement or progression on a page, and fx.slideTo creates a smooth animation of the node in the page, moving it around by specifying the coordinates of the top and left position of the node in pixels.

到目前为止,我们已经知道了如何使用动画隐藏节点,但我们如何一点点移动节点,产生滑动效果呢?fade和wipe效果都没有改变节点的位置。但fx.slideTo函数可以做到这点,改变节点的位置。在页面上,在操作某个节点时,可以让节点按照像素点移动,产生动画效果是很有用的,fx.SlideTo函数就可以通过设置目标坐标的方式,让节点滑动,滑动后,目标坐标将成为节点的左上角坐标。

 <button id="slideAwayButton">Slide block away</button>
<button id="slideBackButton">Slide block back</button> <div id="slideTarget" class="red-block slide">
A red block
</div>
<script>
require(["dojo/fx", "dojo/on", "dojo/dom", "dojo/domReady!"], function(fx, on, dom) {
var slideAwayButton = dom.byId("slideAwayButton"),
slideBackButton = dom.byId("slideBackButton"),
slideTarget = dom.byId("slideTarget"); on(slideAwayButton, "click", function(evt){
fx.slideTo({ node: slideTarget, left: "200", top: "200" }).play();
});
on(slideBackButton, "click", function(evt){
fx.slideTo({ node: slideTarget, left: "0", top: "100" }).play();
});
});
</script>

View Demo

Animation Events 动画事件

As discussed previously, all of these animation methods return a dojo/_base/fx::Animation object. These objects all provide not just controls to play or pause the animation, but they also provide a set of events that we can listen to, in order to perform some sorts of actions before, during, and after the animation. Two of the most important and commonly-used event handlers are beforeBegin and onEnd:

就如我们前面讨论的,所有的动画函数都返回dojo/_base/fx::Animation对象。这个对象不仅提供了控制play、或者pause动画的函数功能,而且还提供了动画事件监听接口,例如动作开始事件、动作执行事件以及动作执行完毕等事件。这些事件中,我们用的最多的就是beforeBegion和onEnd事件。

 <button id="slideAwayButton">Slide block away</button>
<button id="slideBackButton">Slide block back</button> <div id="slideTarget" class="red-block slide">
A red block
</div>
<script>
require(["dojo/fx", "dojo/on", "dojo/dom-style", "dojo/dom", "dojo/domReady!"], function(fx, on, style, dom) { var slideAwayButton = dom.byId("slideAwayButton"),
slideBackButton = dom.byId("slideBackButton"),
slideTarget = dom.byId("slideTarget"); on(slideAwayButton, "click", function(evt){
// Note that we're specifying the beforeBegin as a property of the animation
// rather than using connect. This ensures that our beforeBegin handler
// executes before any others.
var anim = fx.slideTo({
node: slideTarget,
left: "200",
top: "200",
beforeBegin: function(){ console.warn("slide target is: ", slideTarget); style.set(slideTarget, {
left: "0px",
top: "100px"
});
}
}); // We could have also specified onEnd above alongside beforeBegin,
// but it's just as easy to connect like so
on(anim, "End", function(){
style.set(slideTarget, {
backgroundColor: "blue"
});
}, true); // Don't forget to actually start the animation!
anim.play();
}); on(slideBackButton, "click", function(evt){
var anim = fx.slideTo({
node: slideTarget,
left: "0",
top: "100",
beforeBegin: function(){ style.set(slideTarget, {
left: "200px",
top: "200px"
});
}
}); on(anim, "End", function(){
style.set(slideTarget, {
backgroundColor: "red"
});
}, true); anim.play();
});
});
</script>

You might have noticed that beforeBegin is being passed as a property of the arguments object. The reason for passing it in more directly is that certain animations connect to beforeBegin when they are created. Therefore, if you connect to beforeBegin after the animation is created, your handler will be executed after the animation's beforeBegin handler, which may not be what you want to happen. By passing your handler as a property of the arguments object, you guarantee that your handler will execute first.

你可能注意到了,beforeBegin已经作为一个参数属性传给了动画函数。这样做的话,当该动画对象创建,beforeBegin事件响应的函数就和动画关联到了一起。 Therefore, if you connect to beforeBegin after the animation is created, your handler will be executed after the animation's beforeBegin handler, which may not be what you want to happen. By passing your handler as a property of the arguments object, you guarantee that your handler will execute first.(因此,如果你在动画对象创建之后哦,在设置动画对象的beforeBegin事件,你可能会得不到你想要的结果。基于参数传递,把引用作为参数的一个属性传递的函数中,你可以在第一事件执行该函数)。这地方感觉说的有点不对。

View Demo

Chaining 链接

What if we want to fire animations in sequence? We could use the End event that we just talked about to set up the next effect, but that's not very convenient. This sort of pattern is common enough that the dojo/fx module gives us a couple of great convenience methods to set up effects to run in sequence or in parallel, and each method returns a new dojo/_base/fx::Animation object with its own set of events and methods that represent the entire sequence. Let's first take a look at fx.chain to play animations one after another:

如果我们想按照顺序执行动画呢?我们可以监听第一个动画的End事件,得到后,我们在执行下一个动画,但这样使用起来太不方便了。这种需求很常见,所以dojo/fx模块也为我们提供了相应的API,建立一个动画列表,并可以按照顺序执行。每个动画函数依然会返回一个dojo/_base/fx::Animation对象,这些对象会把时间事件和下一个动画的函数结合在一起,形成一个顺序链。让我们看下fx.chain如何把多个动画函数一个一个按顺序执行的:

 <button id="slideAwayButton">Slide block away</button>
<button id="slideBackButton">Slide block back</button> <div id="slideTarget" class="red-block slide chain">
A red block
</div>
<script>
require(["dojo/_base/fx", "dojo/fx", "dojo/on", "dojo/dom", "dojo/domReady!"], function(baseFx, fx, on, dom) { var slideAwayButton = dom.byId("slideAwayButton"),
slideBackButton = dom.byId("slideBackButton"),
slideTarget = dom.byId("slideTarget"); // Set up a couple of click handlers to run our chained animations
on(slideAwayButton, "click", function(evt){
fx.chain([
baseFx.fadeIn({ node: slideTarget }),
fx.slideTo({ node: slideTarget, left: "200", top: "200" }),
baseFx.fadeOut({ node: slideTarget })
]).play();
});
on(slideBackButton, "click", function(evt){
fx.chain([
baseFx.fadeIn({ node: slideTarget }),
fx.slideTo({ node: slideTarget, left: "0", top: "100" }),
baseFx.fadeOut({ node: slideTarget })
]).play();
});
});
</script>

As you can see here, we create a few effects directly inline the call to fx.chain, and immediately call play on the returned dojo/_base/fx::Animation to start the chained animation. We don't start playing each individual effect within the chain: fx.chain handles that for us.

正如你看到的那样,我们创建了我们在fx.chain函数中创建几个效果,并且调用了该函数返回的dojo/_base/fx::Animation对象的play函数,开始执行chined的动画。在这里,我们并没有一个一个的去执行chain函数中包含的动画对象,而是fx.chain帮我们处理了这些。

View Demo

Combining 结合

The second convenience method that dojo/fx provides is the combine method which will start multiple animations at the same time. The dojo/_base/fx::Animation returned will fire its onEnd event after the animation with the longest duration finishes. Let's look at another example:

dojo/fx模块提供的第二个方便的函数就是combine函数,该函数可以组织多个动画同事执行。该函数依然返回dojo/_base/fx::Animation对象,该对象可以监听整个动画的end事件。当执行事件最长的哪个动画执行完毕之后,该对象的End事件将会被触发。让我们看下例子:

 <button id="slideAwayButton">Slide block away</button>
<button id="slideBackButton">Slide block back</button> <div id="slideTarget" class="red-block slide chain">
A red block
</div>
<script>
require(["dojo/_base/fx", "dojo/fx", "dojo/on", "dojo/dom", "dojo/domReady!"], function(baseFx, fx, on, dom) { var slideAwayButton = dom.byId("slideAwayButton"),
slideBackButton = dom.byId("slideBackButton"),
slideTarget = dom.byId("slideTarget"); // Set up a couple of click handlers to run our combined animations
on(slideAwayButton, "click", function(evt){
fx.combine([
baseFx.fadeIn({ node: slideTarget }),
fx.slideTo({ node: slideTarget, left: "200", top: "200" })
]).play();
});
on(slideBackButton, "click", function(evt){
fx.combine([
fx.slideTo({ node: slideTarget, left: "0", top: "100" }),
baseFx.fadeOut({ node: slideTarget })
]).play();
});
});
</script>

In this case, rather than playing the fade and then the slideTo in sequence, they play simultaneously.

在这个例子中,不是先淡出,然后再移动目标节点,二是同时执行。

Using fx.chain and fx.combine, you can build up some fairly elaborate animation sequences. Also, given that both chain and combine return animation objects, the end results of those methods can also be chained and combined, allowing for you to build everything from very simple animations to very rich and detailed sequences.

使用fx.chain和fx.combine,你可以创建一些相对复杂的动画序列。并且,使用两个函数返回的动画对象依然可以组合成顺序执行的动画组或者同时执行的动画组。根据这个特性,你就可以一个个简单的动画,通过多种方式组合成一个相当负责的动画。

View Demo

Conclusion 小结

Using Dojo, it's quite simple to add some flair to your pages. With the dojo/_base/fx and dojo/fx modules, you can easily fade DOM nodes in and out, and with a simple require, you can bring in a lot more powerful ways to simply and easily move your nodes about or wipe them in and out, and the ability to chain and combine animations means you can quickly and easily build up a rather advanced animation.

However, what if you want to do something more advanced, like adjusting a DOM node's height but not necessarily shrinking it to zero, or perhaps adjusting a background color via animation? There's the more generalized fx.animateProperty, which we'll be covering in the next tutorial.

使用dojo,你可以快速简单的在你的页面上添加一些亮点。使用dojo/_base/fx和dojo/fx模块,你可以简便的淡入淡出DOM节点。可以使用简单的代码就可以让节点移动、擦除等。使用chain和combine动画组合,可以让你把一些简单的动画组合在一起,从而创建出高级复杂的动画。

Dojo特效(翻译)的更多相关文章

  1. Events with Dojo(翻译)

    In this tutorial, we will be exploring dojo/on and how Dojo makes it easy to connect to DOM events. ...

  2. 开始使用DOJO(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/start/index.html 我怎么开始学习DOJO?文档在哪?我如何获取支持和培训?我应该 ...

  3. 现代DOJO(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/modern_dojo/index.html 你可能已经不用doio一段时间了,或者你一直想保持 ...

  4. 使用dojoConfig配置dojo(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/dojo_config/index.html dojoConfig对象(原来是djConfig对 ...

  5. dojo 官方翻译 dojo/Deferred

    延迟,异步调用 官网地址:http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html require(["dojo/Defer ...

  6. dojo 官方翻译 dojo/aspect

    官网地址:http://dojotoolkit.org/reference-guide/1.10/dojo/aspect.html after() 定义:after(target, methodNam ...

  7. dojo 官方翻译 dojo/domReady 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/domReady.html#dojo-domready dom加载完成后,执行. requi ...

  8. dojo 官方翻译 dojo/json 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/json.html#dojo-json require(["dojo/json&q ...

  9. dojo 官方翻译 dojo/string 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/string.html#dojo-string require(["dojo/st ...

随机推荐

  1. 深入理解css系列:css定位

    一.概述 1.默认文档流定位方式 (1).HTML默认文档以流模式定位,即内容元素按照先后顺序依次上下定位: (2).HTML标签元素总体分为块状元素.内联元素.内联块状元素,可通过该标签对应的DOM ...

  2. 库函数API和C语言汇编语言混合式编程

    C语言代码内嵌汇编的方法: 在C语言文件中以如下格式加入汇编代码 __asm__( “汇编语句模板” :输出部分 :输入部分 :“破坏描述部分” ) asm可以由__asm__代替,为其别名. 可加上 ...

  3. CSS各种定位详解

    1.定位的专业解释 (1)语法 position:static|absolute|fixed|relative (2)说明 从上面语法可以看出,定位的方法有很多种,它们分别是静态(static),绝对 ...

  4. Deactivate .NET refector

    8.5版本用注册机注册时手快成Standed版本,搞错......,能否Deactivated,发现要联网..... 接下来查找.net reflector 在位置%UserProfile%\AppD ...

  5. 利用 ipset 封禁大量 IP

    使用 iptables 封 IP,是一种比较简单的应对网络攻击的方式,也算是比较常见.有时候可能会封禁成千上万个 IP,如果添加成千上万条规则,在一台注重性能的服务器或者本身性能就很差的设备上,这就是 ...

  6. C语言基础回顾

    第一章 C语言基础 1.  C语言编译过程 预处理:宏替换.条件编译.头文件包含.特殊符号 编译.优化:翻译并优化成等价的中间代码表示或汇编代码 汇编:生成目标文件,及与源程序等效的目标的机器语言代码 ...

  7. 一个assert的写法

    ]; int assert_buf_len; #ifdef XXX_DEBUG #define assert(expr, ...) \ do{ \ if ((!(expr))) \ {\ char * ...

  8. C#编程语言与面向对象—— 多态

    多态编程的基本原理是: 使用基类或接口变量编程. 在多态编程中,基类一般都是抽象类,其中拥有一个或多个抽象方法,各个子类可以根据需要重写这些方法.或者使用接口,每个接口都规定了一个或多个抽象方法,实现 ...

  9. git和SVN交互(待完善)

    右键 git bash here 然后从SVN同步代码,git status命令,用来确认版本库状态 右键 --> TortoiseGit -->  show log 打开日志  查看gi ...

  10. web新内容

    利用css完成如图的排版 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...