Script.aculo.us开发系列(四):动画弹出菜单的实现-开发自己的动画
2010-09-14 13:38:41 来源:WEB开发网在这篇文章中先分析一个案例,然后实现一个类似Amazon或者DangDang上面的动画弹出菜单,谈谈如何利用Script.aculo.us的Effect开发自定义动画,先看看运行效果 http://www1.qcxy.hb.cn/qphy/Script_Aculo_Us/AmazonPopMenu.html
Effect.MoveAndResizeTo = Class.create();//Prototype中创建类的"招式"
/**//*Object.extend()本来是扩展类功能的函数,事实上已经构成了继承,Object.extend(Effect.MoveAndResizeTo.prototype, Effect.Base.prototype)将Effect.Base的所有方法和属性(或者说是原型prototype)扩展到Effect.MoveAndResizeTo.prototype,这样MoveAndResizeTo就有了Base的所有方法和属性,在此基础上在重载initialize()方法和update(),这样自定义动画就生成了*/
Object.extend(Object.extend(Effect.MoveAndResizeTo.prototype, Effect.Base.prototype), {
initialize: function(element, toTop, toLeft, toWidth, toHeight) {//initialize()其实是构造函数
this.element = $(element);
this.toTop = toTop;
this.toLeft = toLeft;
this.toWidth = toWidth;
this.toHeight = toHeight;
/**//*计算元素的坐标和大小,注意默认值的实现,这些都是每个开发者都应该注意的细节*/
this.originalTop = parseFloat(Element.getStyle(this.element,'top') || 0);
this.originalLeft = parseFloat(Element.getStyle(this.element,'left') || 0);
this.originalWidth = parseFloat(Element.getStyle(this.element,'width') || 0);
this.originalHeight = parseFloat(Element.getStyle(this.element,'height') || 0);
this.effectiveTop = this.toTop;
this.effectiveLeft = this.toLeft;
/**//*准确的计算元素的宽度和高度,要考虑两种不同的盒模型,详细可以参考http://hi.baidu.com/fysy/blog/item/97b671f0239b4aaea50f525b.html*/
this.effectiveWidth = this.toWidth
- parseFloat(Element.getStyle(this.element,'margin-left') || 0)
- parseFloat(Element.getStyle(this.element,'margin-right') || 0)
- (document.compatMode == 'BackCompat' ? 0 :parseFloat(Element.getStyle(this.element,'padding-left') || 0)
+ parseFloat(Element.getStyle(this.element,'padding-right') || 0)
+ parseFloat(Element.getStyle(this.element,'border-left-width') || 0)
+ parseFloat(Element.getStyle(this.element,'border-right-width') || 0));
this.effectiveHeight = this.toHeight
- parseFloat(Element.getStyle(this.element,'margin-top') || 0)
- parseFloat(Element.getStyle(this.element,'margin-bottom') || 0)
- (document.compatMode == 'BackCompat' ? 0 :parseFloat(Element.getStyle(this.element,'padding-top') || 0)
+ parseFloat(Element.getStyle(this.element,'padding-bottom') || 0)
+ parseFloat(Element.getStyle(this.element,'border-top-width') || 0)
+ parseFloat(Element.getStyle(this.element,'border-bottom-width') || 0));
/**//*选项的实现,arguments[5]返回第六个参数,如果没有则为空的Json,默认的选项无非是把这里的Json改改*/
this.options = arguments[5] || {};
if (this.effectiveWidth < 0) this.effectiveWidth = 0;
if (this.effectiveHeight < 0) this.effectiveHeight = 0;
if (this.originalTop == this.effectiveTop &&
this.originalLeft == this.effectiveLeft &&
this.originalWidth == this.effectiveWidth &&
this.originalHeight == this.effectiveHeight) {
// no need to start!
return;
}
/**//*在构造函数的最后别忘了加上这么一行,Base不是有一些选项么,起码得设置一下吧.它好像C#中的base.functionName()*/
this.start(this.options);
},
/**//*重写update()方法,多次提到每帧都会调用这个方法重写视图,接受一个参数position 这个position是transition中的属性指向的方法返回的值,介于0到1之间,这样rensition的选项可以在任何动画中使用了*/
update: function(position) {
topd = this.effectiveTop * (position) + this.originalTop * (1 - position);
leftd = this.effectiveLeft * (position) + this.originalLeft * (1 - position);
widthd = this.effectiveWidth * (position) + this.originalWidth * (1 - position);
heightd = this.effectiveHeight * (position) + this.originalHeight * (1 - position);
this.setPosition(topd, leftd, widthd, heightd);
},
setPosition: function(topd, leftd, widthd, heightd) {
this.element.style.top = topd+'px';
this.element.style.left = leftd+'px';
this.element.style.width = widthd+'px';
this.element.style.height = heightd+'px';
}
});
- ››user-agent查询方法
- ››Script.Aculo.Us开发系列(一):起步
- ››Script.aculo.us开发系列(二):EffectsAPI
- ››Script.aculo.us开发系列(三):核心效果(CoreEffec...
- ››Script.aculo.us开发系列(四):动画弹出菜单的实现...
- ››Script.aculo.us开发系列(五):Prototype封装的艺术...
- ››Script.aculo.us开发系列(六):AutoComplete控件
- ››Script.aculo.us开发系列(七):InPlaceEditor控件的...
- ››Script.aculo.us开发系列(八):使用DropDrag定义拖...
- ››Script.aculo.us开发系列(九):Slider控件的使用
- ››userinit.exe出现异常之解决方案
- ››Using The Silverlight DataGrid
更多精彩
赞助商链接