jQuery Easing 動畫效果擴展詳細講解


今天介紹的是jQuery Easing  插件,非常的小。美化排版后是130行左右,要是壓縮了那就更小了。這個插件彌補了jquery里面的動畫效果的不足,是其動畫效果更加強悍。如此強大而且小的插件,如果不好好加以利用,真是浪費。很多國內的朋友就是使用其中的一些效果,幾個簡單的參數。很少有人整理所有效果的參數。下面是我整理出來的:

linear,swing,jswing,easeInQuad,easeOutQuad,easeInOutQuad,easeInCubic,
easeOutCubic,easeInOutCubic,easeInQuart,easeOutQuart,easeInOutQuart,
easeInQuint,easeOutQuint,easeInOutQuint,easeInSine,easeOutSine,
easeInOutSine,easeInExpo,easeOutExpo,easeInOutExpo,easeInCirc,
easeOutCirc,easeInOutCirc,easeInElastic,easeOutElastic,easeInOutElastic,
easeInBack,easeOutBack,easeInOutBack,easeInBounce,easeOutBounce,easeInOutBounce


一共這么多效果參數。字面意思已經很明白,不過想真正清楚動畫的效果還得看演示。



 
The box below will animate using that function and then a graph will appear showing that animation (black dots) against the ideal curve (white line). The  X-axis  is time, the  Y-axis  can be thought of as distance or progression. Each black dot (with a white border) signifies when jQuery actually changed a CSS property. (Note: old curves fade into the background; for comparison)

這段話是原作者的介紹,X軸表示時間,Y軸表示的是動畫效果的變化。查看這些曲線變化,更利於掌握這個插件的效果。
插件支持站:http://gsgd.co.uk/sandbox/jquery/easing/
效果演示站點:http://james.padolsey.com/demos/jquery/easing/
對於想深入研究和只需要效果的朋友,這演示站點絕對是最好的。所有的效果一覽還有上圖示范的動畫參數曲線。
 
如果你曾經使用過這個插件,下面的話就沒有必要看了。直接去查看曲線及效果,運用到自己的站點吧。

下面介紹這個插件的簡單使用方法,
查看jqueryAPI文檔可以看到自定義動畫函數.animate()有四個參數:
params (Options) : 一組包含作為動畫屬性和終值的樣式屬性和及其值的集合 duration (String,Number) : (可選) 三種預定速度之一的字符串(”slow”, “normal”, or “fast”)或表示動畫時長的毫秒數值(如:1000) easing (String) : (可選) 要使用的擦除效果的名稱(需要插件支持).默認jQuery提供”linear” 和 “swing”. callback (Function) : (可選) 在動畫完成時執行的函數
其中參數easing默認有兩個效果:“linear”和“swing”,需要更多的效果就要用上這個插件jQuery Easing 

上面列出的就是所有的動畫效果,使用方法首先當然是調用我們簡單易用的jquery 其次是這個插件。考慮到這個插件很小,可以直接寫在自己的js文檔里面。

開始使用jQuery Easing
 
方法1、設置jQuery默認動畫效果
1 jQuery.easing.def = “method”;//如:easeOutExpo

 

方法2、jQuery默認動畫

支持toggle()、slideUp()、slideDown()、show()、hide()等jQuery內置的動畫效果

如以下代碼:
1 $(element).slideUp({
2     duration: 1000, 
3     easing: method, 
4     complete: callback
5 });

 

方法3、使用jQuery自定義動畫函數.animate()
jQuery 的.animate()是自定義動畫的函數,如上面所說,有四個參數,而其中easing的參數就是我們進行動畫效果擴展的方法名稱(如easeOutExpo)。最簡單的使用方法是:

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

 

James Padolsey對jQuery1.3.2的animate函數進行了修改擴展:

 1 jQuery.fn.animate = (function(_anim){
 2     var jQEasing = jQuery.easing;
 3     return function(prop, duration, easing, callback) {
 4         var props = {}, optall, i, hasEaser = false;
 5         for ( i in prop ) {
 6             if ( jQuery.isArray(prop[i]) ) {
 7                 hasEaser = true;
 8                 props[i] = prop[i][1];
 9                 prop[i] = prop[i][0];
10             }
11         }
12         opt = jQuery.speed(duration, easing, callback);
13         if (hasEaser) {
14             opt.step = (function(_step){
15                 return function(now, fx) {
16                     var end = fx.end, easeFn;
17                     if ( easeFn = props[fx.prop] ) {
18                         fx.now = jQEasing[easeFn]( now/end, now, 0, end, end );
19                     }
20                     _step && _step.call( fx.elem, fx.now, fx );
21                 };
22             })(opt.step);
23         }
24         opt.complete = opt.old || callback || jQuery.isFunction(easing) && easing;
25         return _anim.call( this, prop, opt );
26     };
27 })(jQuery.fn.animate);

在jQuery1.4中這種方式已經被引入,所以jQuery1.4中不需要添加jQuery的animate()擴展,我們就可以使用下面的更加方便代碼啦:

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

jQuery1.4 的animate()+Easing

1 jQuery(myElement).animate({
2     left: [500, 'swing'],
3     top: [200, 'easeOutBounce']
4 });

或者:

1  jQuery(myElement).animate({
2        left: 500,
3        top: 200
4 }, {
5        specialEasing: {
6             left: 'swing',
7             top: 'easeOutBounce'
8         }
9 });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM