This repository has been archived by the owner on Nov 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
jquery.pause.js
94 lines (90 loc) · 2.45 KB
/
jquery.pause.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*!
* Pause jQuery plugin v0.1
*
* Copyright 2010 by Tobia Conforto <[email protected]>
*
* 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)]);
}
});
};
})();