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