博客园 :: 首页 ::  ::  ::  :: 管理
续上回:  怎样写一个通用的JavaScript效果库!(1/2) 

在上个随笔中贴出了效果库的整体框架,和一个简单的opacity插件. 今天这个随笔主要是扩展其他常用
效果插件,毕竟框架只能是个空壳,内容还是要自己充实。
如果看过了我上篇的实现细节,这里就不多说废话了,来段代码先:

/****************************************************/

// 移动, 这里是move to  就是移动到 x,y 当然,大家也可以再扩展一个move by  移动x个象素
Effect.Init.move=function(effect){   //初始化
    if (effect.options.x!==undefined || effect.options.y!==undefined){        
        
var pos=Position.cumulativeOffset(effect.element);
        effect.setting.left       
=pos[0];
        effect.setting.top          
=pos[1];
        effect.setting.position 
=effect.element.style.position;     
        effect.element.style.position    
="absolute"
        effect.options.x
=(effect.options.x===undefined)?effect.setting.left:effect.options.x;
        effect.options.y
=(effect.options.y===undefined)?effect.setting.top :effect.options.y;                        
    }

}


Effect.Fn.move
=function(effect,pos){     //效果
    if (effect.options.x===undefined && effect.options.y===undefined) return        
    effect.element.style.left
=effect.setting.left + (effect.options.x-effect.setting.left) * pos +"px";
    effect.element.style.top 
=effect.setting.top  + (effect.options.y-effect.setting.top ) * pos +"px";
}

/****************************************************/




/****************************************************/
// zoom   by Go_Rush(阿舜) from http://ashun.cnblogs.com/
Effect.Init.zoom=function(effect){    
    effect.setting.zoom      
=effect.element.style.zoom || 1;
    
// firefox 不支持 css的 zoom 用  改变 width,height的方式代替
    if (effect.options.zoom!==undefined && navigator.userAgent.toLowerCase().indexOf('firefox') != -1){                    
        effect.options.w
=effect.element.offsetWidth  * effect.options.zoom;
        effect.options.h
=effect.element.offsetHeight * effect.options.zoom;    
    }

}

Effect.Fn.zoom
=function(effect,pos){
    
if (effect.options.zoom===undefined) return;
    effect.element.style.zoom
=effect.setting.zoom+(effect.options.zoom-effect.setting.zoom)*pos
}


/****************************************************/


/****************************************************/
// size  同上,是 size to, 改变到指定大小 by Go_Rush(阿舜) from http://ashun.cnblogs.com/
Effect.Init.size=function(effect){
    
if (effect.options.w!==undefined || effect.options.h!==undefined){
        effect.setting.overflow   
=effect.element.style.overflow || 'visible';
        effect.setting.width      
=effect.element.offsetWidth;
        effect.setting.height      
=effect.element.offsetHeight; 
        effect.element.style.overflow 
="hidden"    
        effect.options.w
=(effect.options.w===undefined)?effect.setting.width :effect.options.w;
        effect.options.h
=(effect.options.h===undefined)?effect.setting.height:effect.options.h;            
    }

}


Effect.Fn.size
=function(effect,pos){    
    
if (effect.options.w===undefined && effect.options.h===undefined) return;
    effect.element.style.width 
=effect.setting.width + (effect.options.w-effect.setting.width ) * pos +"px";
    effect.element.style.height
=effect.setting.height+ (effect.options.h-effect.setting.height) * pos +"px";
}

/****************************************************/


/****************************************************/
// 背景色 by Go_Rush(阿舜) from http://ashun.cnblogs.com/
Effect.Init.bgcolor=function(effect){
    
if (effect.options.bgcolor!==undefined && /^\#?[a-f0-9]{6}$/i.test(effect.options.bgcolor)){
        
var color =effect.element.style.backgroundColor || "#ffffff";
        
//FireFox 下,即使css样式设置背景为 #ffffff格式,但程序取到的是 rgb(255,255,255)格式, 这里把他转化为 #ffffff格式
        if (/rgb/i.test(color)){               // "rgb(255, 0, 255)"
            //var arr=color.replace(/[rgb\(\s\)]/gi,"").split(",")
            var arr=eval(color.replace("rgb","new Array"))       
            color
="#"+Number(arr[0]).toColorPart()+Number(arr[1]).toColorPart()+Number(arr[2]).toColorPart()
        }

        effect.setting.bgcolor
=color
    }

}

Effect.Fn.bgcolor
=function(effect,pos){    
    
if (effect.options.bgcolor===undefined) return;
    
var c1=effect.setting.bgcolor,c2=effect.options.bgcolor
    
var arr1=[parseInt(c1.slice(1,3),16),parseInt(c1.slice(3,5),16),parseInt(c1.slice(5),16)]
    
var arr2=[parseInt(c2.slice(1,3),16),parseInt(c2.slice(3,5),16),parseInt(c2.slice(5),16)]
    
var r=Math.round(arr1[0]+(arr2[0]-arr1[0])*pos)
    
var g=Math.round(arr1[1]+(arr2[1]-arr1[1])*pos)
    
var b=Math.round(arr1[2]+(arr2[2]-arr1[2])*pos)
    effect.element.style.backgroundColor
="#"+r.toColorPart()+g.toColorPart()+b.toColorPart()
}

/****************************************************/


/****************************************************/
// 透明度,这个上个贴过了   by Go_Rush(阿舜) from http://ashun.cnblogs.com/
Effect.Init.opacity=function(effect){
    
if (effect.options.opacity===undefined) return;
    effect.setting.opacity
=Opacity(effect.element);    
}


Effect.Fn.opacity
=function(effect,pos){
    
if (effect.options.opacity===undefined) return;
    Opacity(effect.element,effect.setting.opacity
+(effect.options.opacity-effect.setting.opacity)*pos);    
}

/****************************************************/

这里 effect.setting 是非常有用而且非常重要的冬冬,所有的通过options传进来自定义函数都可以
通过effect.setting来获取element最初的设置。 在很多场合,我们需要在 options 中传一个 onComplete
函数进来, 用来在效果执行完毕后,打扫战场,恢复一些设置。
这些效果是可以重叠的,大家可以看看下面我写的例子。

写了十来个例子,应该很详细了。

完整的,可调试代码和例子如下:

全部代码附范例

Feedback

#1楼   回复  引用    

2007-01-16 11:13 by PP[未注册用户]
貌似有个东东叫:mootools
http://mootools.net/download/release

#2楼   回复  引用    

2007-01-16 11:24 by robinhood007[未注册用户]
在loop方法中,用如下操作:

for(var key in Effect.Fn){
if (typeof(Effect.Fn[key])!="function") continue;
try{Effect.Fn[key](effect,pos)}catch(x){}

感觉每个效果都要执行Effect.Fn中的每个函数一遍,并且Effect.Fn会因扩展而变得比较大 这样做效率是否能保证?
能否根据option中的设置选择执行?

#3楼[楼主]   回复  引用  查看    

2007-01-16 12:32 by Go_Rush      
@robinhood007
这个问题问到点上了,其实现在这种做法的效率,也不太影响效率。
关键看写扩展的人怎么写了。

有没有注意到,我的 Effect.Fn 的方法里面所有函数第一句话,都是这样写的
if (effect.options.xxxxxx===undefined) return;
建议每个扩展都这样写,这样 执行 loop函数的时候,对于不相关的效果函数
执行到第一句就跳出了。而避免了每个函数都真正执行一次

#4楼[楼主]   回复  引用  查看    

2007-01-16 12:42 by Go_Rush      
当然,还是有办法让 loop选择性的执行真正要执行的函数。

在initialize 方法里面添加一个数组,来保存loop要执行的函数名
initialize: function(element,options) {
this.element = $(element);
this.options = options || {};
this.fnList =[]; //添加这句


我们可以在每个Init方法里面这样写

Effect.Init.opacity=function(effect){
if (effect.options.opacity===undefined) return;
effect.fnList.push("opacity") //添加这句 添加 opacity方法
......

Effect.Init.size=function(effect){
if (effect.options.w!==undefined || effect.options.h!==undefined){
effect.fnList.push("size") //添加这句 添加 size方法


最后loop方法这样写:

loop:function(effect,pos){ //执行所有效果单元
for(var =0;i<effect.fnList.length;i++){
Effect.Fn[effect.fnList[i]](effect,pos)
}
}

这样的话,就可以在程序运行时根据参数 options 来执行 只相关的效果。

#5楼   回复  引用    

2007-01-16 16:37 by sdfsdf[未注册用户]
loop:function(effect,pos){ //执行所有效果单元
for(var =0;i<effect.fnList.length;i++){
Effect.Fn[effect.fnList[i]](effect,pos)
}
}
?????不行

#6楼[楼主]   回复  引用  查看    

2007-01-16 17:06 by Go_Rush      
@sdfsdf
代码是可以的,我是测试通过才回复的。

并不是只有这一个地方需要修改
loop:function(effect,pos){ //执行所有效果单元

还有几个地方要改
Effect.initialize 添加 this.fnList =new Array();

和每个 Effect.Init.... 都要修改,添加 effect.fnList.push("效果名") ..



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 621122




相关文章:

相关链接: