forked from Keyframes/jQuery.Keyframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.keyframes.js
More file actions
231 lines (194 loc) · 6.89 KB
/
Copy pathjquery.keyframes.js
File metadata and controls
231 lines (194 loc) · 6.89 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
(function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
"use strict";
var _core = _interopRequireDefault(require("@keyframes/core"));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
(function () {
var doForEach = function doForEach($el, cb) {
$el.each(function (index, elem) {
if (elem.Keyframes) {
cb(elem.Keyframes);
} else {
elem.Keyframes = new _core.default(elem);
cb(elem.Keyframes);
}
});
};
$.keyframe = {
isSupported: _core.default.isSupported,
generate: _core.default.generate,
define: _core.default.define
};
$.fn.resetKeyframe = function (cb) {
doForEach(this, function (kf) {
return kf.reset(cb);
});
};
$.fn.pauseKeyframe = function () {
doForEach(this, function (kf) {
return kf.pause();
});
};
$.fn.resumeKeyframe = function () {
doForEach(this, function (kf) {
return kf.resume();
});
};
$.fn.playKeyframe = function (frameOptions, callback) {
doForEach(this, function (kf) {
return kf.play(frameOptions, callback);
});
};
})();
},{"@keyframes/core":2}],2:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Keyframes =
/*#__PURE__*/
function () {
function Keyframes(elem) {
_classCallCheck(this, Keyframes);
this.elem = elem;
}
_createClass(Keyframes, [{
key: "isSupported",
value: function isSupported() {
return document.body.style.animationName !== undefined;
}
}, {
key: "reset",
value: function reset(callback) {
this.removeEvents();
this.elem.style.animationPlayState = 'running';
this.elem.style.animation = 'none';
if (callback) {
setTimeout(callback, 0);
}
}
}, {
key: "pause",
value: function pause() {
this.elem.style.animationPlayState = 'paused';
}
}, {
key: "resume",
value: function resume() {
this.elem.style.animationPlayState = 'running';
}
}, {
key: "play",
value: function play(frameOptions, callback) {
var _this = this;
if (this.elem.style.animationName === frameOptions.name) {
this.reset(function () {
return _this.play(frameOptions, callback);
});
return this;
}
var animObjToStr = function animObjToStr(obj) {
var newObj = Object.assign({}, {
duration: '0s',
timingFunction: 'ease',
delay: '0s',
iterationCount: 1,
direction: 'normal',
fillMode: 'forwards'
}, obj);
return [newObj.name, newObj.duration, newObj.timingFunction, newObj.delay, newObj.iterationCount, newObj.direction, newObj.fillMode].join(' ');
};
var animationcss = '';
if (frameOptions.constructor === Array) {
var frameOptionsStrings = [];
for (var i = 0; i < frameOptions.length; i += 1) {
frameOptionsStrings.push(typeof frameOptions[i] === 'string' ? frameOptions[i] : animObjToStr(frameOptions[i]));
}
animationcss = frameOptionsStrings.join(', ');
} else if (typeof frameOptions === 'string') {
animationcss = frameOptions;
} else {
animationcss = animObjToStr(frameOptions);
}
var addEvent = function addEvent(type, eventCallback) {
var listenerName = "".concat(type, "Listener");
_this.elem.removeEventListener(type, _this[listenerName]);
_this[listenerName] = eventCallback;
_this.elem.addEventListener(type, _this[listenerName]);
};
this.elem.style.animationPlayState = 'running';
this.elem.style.animation = animationcss;
this.frameOptions = frameOptions;
addEvent('animationiteration', callback || frameOptions.complete);
addEvent('animationend', callback || frameOptions.complete);
return this;
}
}, {
key: "removeEvents",
value: function removeEvents() {
this.elem.removeEventListener('animationiteration', this.animationiterationListener);
this.elem.removeEventListener('animationend', this.animationendListener);
}
}], [{
key: "createKeyframeTag",
value: function createKeyframeTag(id, css) {
var elem = document.createElement('style');
elem.innerHTML = css;
elem.setAttribute('class', 'keyframe-style');
elem.setAttribute('id', id);
elem.setAttribute('type', 'text/css');
document.getElementsByTagName('head')[0].appendChild(elem);
}
}, {
key: "generate",
value: function generate(frameData) {
var frameName = frameData.name || '';
var css = "@keyframes ".concat(frameName, " {");
for (var key in frameData) {
if (key !== 'name' && key !== 'media' && key !== 'complete') {
css += "".concat(key, " {");
for (var property in frameData[key]) {
css += "".concat(property, ":").concat(frameData[key][property], ";");
}
css += '}';
}
}
if (frameData.media) {
css = "@media ".concat(frameData.media, "{").concat(css, "}");
}
var kfTagId = "Keyframes".concat(frameName);
var frameStyle = document.getElementById(kfTagId);
if (frameStyle) {
frameStyle.innerHTML = css;
} else {
Keyframes.createKeyframeTag(kfTagId, css);
}
}
}, {
key: "define",
value: function define(frameData) {
if (frameData.length) {
for (var i = 0; i < frameData.length; i += 1) {
this.generate(frameData[i]);
}
} else {
this.generate(frameData);
}
}
}, {
key: "plugin",
value: function plugin(pluginFunc) {
pluginFunc(Keyframes);
}
}]);
return Keyframes;
}();
exports.default = Keyframes;
},{}]},{},[1]);