從jQuery API 文檔中能夠知道,jQuery自定義動畫的函數.animate( properties [, duration] [, easing] [, complete] )有四個參數:javascript
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script> <script type="text/javascript" src="http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js"></script>
引入以後,easing參數可選的值就有如下32種:html
jQuery.extend( jQuery.easing, { easeOutExpo: function (x, t, b, c, d) { return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; }, easeOutBounce: function (x, t, b, c, d) { if ((t/=d) < (1/2.75)) { return c*(7.5625*t*t) + b; } else if (t < (2/2.75)) { return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b; } else if (t < (2.5/2.75)) { return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b; } else { return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; } }, });
使用jQuery自定義動畫函數animate來指定easing效果,如自定義一種類彈簧效果的動畫:java
$(myElement).animate({ top: 500, opacity: 1 }, 1000, 'easeOutBounce');
值得一提的是jQuery 1.4版本中對animate()方法,easing的方法進行了擴展,支持爲每一個屬性指定easing方法,詳細請參考這裏,如:jquery
//第一種寫法 $(myElement).animate({ left: [500, 'swing'], top: [200, 'easeOutBounce'] }); //第二種寫法 $(myElement).animate({ left: 500, top: 200 }, { specialEasing: { left: 'swing', top: 'easeOutBounce' } });
使用jQuery內置動畫函數如slideUp()、slideDown()等來指定easing效果,如下兩種方法均可以:api
$(myElement).slideUp(1000, method, callback}); $(myElement).slideUp({ duration: 1000, easing: method, complete: callback });