Chris@5
|
1 /*!
|
Chris@5
|
2 * Pause jQuery plugin v0.1
|
Chris@5
|
3 *
|
Chris@5
|
4 * Copyright 2010 by Tobia Conforto <tobia.conforto@gmail.com>
|
Chris@5
|
5 *
|
Chris@5
|
6 * Based on Pause-resume-animation jQuery plugin by Joe Weitzel
|
Chris@5
|
7 *
|
Chris@5
|
8 * This program is free software; you can redistribute it and/or modify it
|
Chris@5
|
9 * under the terms of the GNU General Public License as published by the Free
|
Chris@5
|
10 * Software Foundation; either version 2 of the License, or(at your option)
|
Chris@5
|
11 * any later version.
|
Chris@5
|
12 *
|
Chris@5
|
13 * This program is distributed in the hope that it will be useful, but WITHOUT
|
Chris@5
|
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
Chris@5
|
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
Chris@5
|
16 * more details.
|
Chris@5
|
17 *
|
Chris@5
|
18 * You should have received a copy of the GNU General Public License along with
|
Chris@5
|
19 * this program; if not, write to the Free Software Foundation, Inc., 51
|
Chris@5
|
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@5
|
21 */
|
Chris@5
|
22 /* Changelog:
|
Chris@5
|
23 *
|
Chris@5
|
24 * 0.1 2010-06-13 Initial release
|
Chris@5
|
25 */
|
Chris@5
|
26 (function() {
|
Chris@5
|
27 var $ = jQuery,
|
Chris@5
|
28 pauseId = 'jQuery.pause',
|
Chris@5
|
29 uuid = 1,
|
Chris@5
|
30 oldAnimate = $.fn.animate,
|
Chris@5
|
31 anims = {};
|
Chris@5
|
32
|
Chris@5
|
33 function now() { return new Date().getTime(); }
|
Chris@5
|
34
|
Chris@5
|
35 $.fn.animate = function(prop, speed, easing, callback) {
|
Chris@5
|
36 var optall = $.speed(speed, easing, callback);
|
Chris@5
|
37 optall.complete = optall.old; // unwrap callback
|
Chris@5
|
38 return this.each(function() {
|
Chris@5
|
39 // check pauseId
|
Chris@5
|
40 if (! this[pauseId])
|
Chris@5
|
41 this[pauseId] = uuid++;
|
Chris@5
|
42 // start animation
|
Chris@5
|
43 var opt = $.extend({}, optall);
|
Chris@5
|
44 oldAnimate.apply($(this), [prop, $.extend({}, opt)]);
|
Chris@5
|
45 // store data
|
Chris@5
|
46 anims[this[pauseId]] = {
|
Chris@5
|
47 run: true,
|
Chris@5
|
48 prop: prop,
|
Chris@5
|
49 opt: opt,
|
Chris@5
|
50 start: now(),
|
Chris@5
|
51 done: 0
|
Chris@5
|
52 };
|
Chris@5
|
53 });
|
Chris@5
|
54 };
|
Chris@5
|
55
|
Chris@5
|
56 $.fn.pause = function() {
|
Chris@5
|
57 return this.each(function() {
|
Chris@5
|
58 // check pauseId
|
Chris@5
|
59 if (! this[pauseId])
|
Chris@5
|
60 this[pauseId] = uuid++;
|
Chris@5
|
61 // fetch data
|
Chris@5
|
62 var data = anims[this[pauseId]];
|
Chris@5
|
63 if (data && data.run) {
|
Chris@5
|
64 data.done += now() - data.start;
|
Chris@5
|
65 if (data.done > data.opt.duration) {
|
Chris@5
|
66 // remove stale entry
|
Chris@5
|
67 delete anims[this[pauseId]];
|
Chris@5
|
68 } else {
|
Chris@5
|
69 // pause animation
|
Chris@5
|
70 $(this).stop();
|
Chris@5
|
71 data.run = false;
|
Chris@5
|
72 }
|
Chris@5
|
73 }
|
Chris@5
|
74 });
|
Chris@5
|
75 };
|
Chris@5
|
76
|
Chris@5
|
77 $.fn.resume = function() {
|
Chris@5
|
78 return this.each(function() {
|
Chris@5
|
79 // check pauseId
|
Chris@5
|
80 if (! this[pauseId])
|
Chris@5
|
81 this[pauseId] = uuid++;
|
Chris@5
|
82 // fetch data
|
Chris@5
|
83 var data = anims[this[pauseId]];
|
Chris@5
|
84 if (data && ! data.run) {
|
Chris@5
|
85 // resume animation
|
Chris@5
|
86 data.opt.duration -= data.done;
|
Chris@5
|
87 data.done = 0;
|
Chris@5
|
88 data.run = true;
|
Chris@5
|
89 data.start = now();
|
Chris@5
|
90 oldAnimate.apply($(this), [data.prop, $.extend({}, data.opt)]);
|
Chris@5
|
91 }
|
Chris@5
|
92 });
|
Chris@5
|
93 };
|
Chris@5
|
94 })(); |