/*! * Pause jQuery plugin v0.1 * * Copyright 2010 by Tobia Conforto * * Based on Pause-resume-animation jQuery plugin by Joe Weitzel * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or(at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* Changelog: * * 0.1 2010-06-13 Initial release */ (function() { var $ = jQuery, pauseId = 'jQuery.pause', uuid = 1, oldAnimate = $.fn.animate, anims = {}; function now() { return new Date().getTime(); } $.fn.animate = function(prop, speed, easing, callback) { var optall = $.speed(speed, easing, callback); optall.complete = optall.old; // unwrap callback return this.each(function() { // check pauseId if (! this[pauseId]) this[pauseId] = uuid++; // start animation var opt = $.extend({}, optall); oldAnimate.apply($(this), [prop, $.extend({}, opt)]); // store data anims[this[pauseId]] = { run: true, prop: prop, opt: opt, start: now(), done: 0 }; }); }; $.fn.pause = function() { return this.each(function() { // check pauseId if (! this[pauseId]) this[pauseId] = uuid++; // fetch data var data = anims[this[pauseId]]; if (data && data.run) { data.done += now() - data.start; if (data.done > data.opt.duration) { // remove stale entry delete anims[this[pauseId]]; } else { // pause animation $(this).stop(); data.run = false; } } }); }; $.fn.resume = function() { return this.each(function() { // check pauseId if (! this[pauseId]) this[pauseId] = uuid++; // fetch data var data = anims[this[pauseId]]; if (data && ! data.run) { // resume animation data.opt.duration -= data.done; data.done = 0; data.run = true; data.start = now(); oldAnimate.apply($(this), [data.prop, $.extend({}, data.opt)]); } }); }; })();