jQuery Easing动画效果扩展插件

在jQuery文档中我们可以看到,自定义动画函数.animate()有四个参数:

params (Options) : 一组包含作为动画属性和终值的样式属性及其值的集合
duration (String,Number) : (可选) 三种预定速度之一的字符串(”slow”, “normal”, or “fast”)或表示动画时长的毫秒数值(如:1000)
easing (String) : (可选) 要使用的擦除效果的名称(需要插件支持).默认jQuery提供”linear” 和 “swing”.
callback (Function) : (可选) 在动画完成时执行的函数

其中参数easing默认有两个效果:“linear”和“swing”,如果需要更多就要jQuery Easing Plugin插件支持了,

在jQuery Easing中设置了easeInElastic、easeOutElastic、easeInOutElastic等共31中不同的效果.

引入Easing js文件
要使用jQuery Easing扩展,首先我们要在jQuery之后,引入jQuery Easing Plugin文件,如以下代码
jQuery Easing简单教程
方法1、设置jQuery默认动画效果

jQuery.easing.def = “method”;//如:easeOutExpo

方法2、jQuery默认动画

支持toggle()、slideUp()、slideDown()、show()、hide()等jQuery内置的动画效果

如以下代码:

$(element).slideUp({
 duration: 1000,
 easing: method,
 complete: callback});

方法3、使用jQuery自定义动画函数.animate()

jQuery 的.animate()是自定义动画的函数,如上面所说,有四个参数,而其中easing的参数就是我们进行动画效果扩展的方法名称(如easeOutExpo)。最简单的使用方法是:

$(myElement).animate({
     left: 500,
     top: 500
 }, 'easeOutExpo');

上面的代码就是实现的动画之后以easeOutExpo的方法来进行缓冲(easing),这是animate() easing的基本用法

James Padolsey对jQuery1.3.2的animate函数进行了修改扩展:

jQuery.fn.animate = (function(_anim){
     var jQEasing = jQuery.easing;
     return function(prop, duration, easing, callback) {
         var props = {}, optall, i, hasEaser = false;
         for ( i in prop ) {
             if ( jQuery.isArray(prop[i]) ) {
                 hasEaser = true;
                 props[i] = prop[i][1];
                 prop[i] = prop[i][0];
             }
         }
         opt = jQuery.speed(duration, easing, callback);
         if (hasEaser) {
             opt.step = (function(_step){
                 return function(now, fx) {
                     var end = fx.end, easeFn;
                     if ( easeFn = props[fx.prop] ) {
                         fx.now = jQEasing[easeFn]( now/end, now, 0, end, end );
                     }
                     _step && _step.call( fx.elem, fx.now, fx );
                 };
             })(opt.step);
         }
         opt.complete = opt.old || callback || jQuery.isFunction(easing) && easing;
         return _anim.call( this, prop, opt );
     };
 })(jQuery.fn.animate);
 在jQuery1.4中这种方式已经被引入,所以jQuery1.4中不需要添加jQuery的animate()扩展,我们就可以使用下面的更加方便代码啦: 

 $(myElement).animate({
     left: 500,
     top: [500, 'easeOutBounce']
 }, 1000,'swing');

上面的代码的效果是,总体上来使用swing的方法来缓冲,而top的时候采用easeOutBounce的方法来缓冲。

jQuery1.4 的animate()+Easing
值得一提的是jQuery 1.4版本中对animate()方法,easing的方法进行了扩展

jQuery(myElement).animate({
     left: [500, 'swing'],
     top: [200, 'easeOutBounce']
 });
或者
jQuery(myElement).animate({
     left: 500,
     top: 200
 }, {
     specialEasing: {
         left: 'swing',
         top: 'easeOutBounce'
     }
 });
原文地址:http://blog.csdn.net/prettywhitewolf_xiao/article/details/7657151
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值