Skip to content

Commit b5281d7

Browse files
committed
Allow multiple animations. Improved syntax. #26
1 parent d984f88 commit b5281d7

File tree

7 files changed

+79
-47
lines changed

7 files changed

+79
-47
lines changed

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,43 @@ $.keyframe.define([{
132132

133133
**Playing an animation**
134134

135+
The css3 animation methods available are better documented here: http://www.w3schools.com/css/css3_animations.asp
136+
135137
```javascript
136138
$(selector).playKeyframe({
137139
name: 'trapdoor-sequence', // name of the keyframe you want to bind to the selected element
138140
duration: 1000, // [optional, default: 0, in ms] how long you want it to last in milliseconds
139141
timingFunction: 'linear', // [optional, default: ease] specifies the speed curve of the animation
140-
delay: 0, //[optional, default: 0, in ms] how long you want to wait before the animation starts in milliseconds, default value is 0
141-
repeat: 'infinite', //[optional, default:1] how many times you want the animation to repeat, default value is 1
142-
direction: 'normal', //[optional, default: 'normal'] which direction you want the frames to flow, default value is normal
142+
delay: 0, //[optional, default: 0] how long you want to wait before the animation starts
143+
iterationCount: 'infinite', //[optional, default:1] how many times you want the animation to repeat
144+
direction: 'normal', //[optional, default: 'normal'] which direction you want the frames to flow
143145
fillMode: 'forwards', //[optional, default: 'forward'] how to apply the styles outside the animation time, default value is forwards
144-
complete: function(){} //[optional] Function fired after the animation is complete. If repeat is infinite, the function will be fired every time the animation is restarted.
146+
complete: function(){} //[optional] Function fired after the animation is complete. If repeat is infinite, the function will be fired every time the animation is restarted.
145147
});
146148
```
147149

148150
**Playing an animation (shorthand)**
149151

150152
```javascript
151153
$(selector).playKeyframe(
152-
'trapdoor-sequence 1000 linear 0 infinite normal forwards',
154+
'trapdoor-sequence 1s linear 0 infinite normal forwards',
153155
complete
154156
);
155-
```
157+
```
158+
159+
**Playing multiple animations**
160+
161+
```javascript
162+
$(selector).playKeyframe([
163+
'trapdoor-sequence 1s linear 0 infinite',
164+
{
165+
name: 'ball-roll',
166+
duration: "3s",
167+
timingFunction: 'ease',
168+
iterationCount: 1
169+
}
170+
], complete);
171+
```
156172

157173
**Reset the animation**
158174

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jQueryKeyframes",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"homepage": "https://github.com/jQueryKeyframes/jQuery.Keyframes",
55
"license": "MIT",
66
"private": false,

example/advanced/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
<div class="leaf"></div>
1111
</div>
1212
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
13-
<script src="../../jquery.keyframes.min.js"></script>
13+
<script type="text/javascript" src="../../bower_components/prefixfree/prefixfree.min.js"></script>
14+
<script type="text/javascript" src="../../jquery.keyframes.js"></script>
1415
<script src="js/animation.js"></script>
1516
<script src="js/init.js"></script>
1617
</body>

example/advanced/js/init.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(function() {
22
$(window).ready(function() {
3-
$('.cloud').playKeyframe('clouds 50000 linear 0 infinite normal forwards');
4-
$('.balloon').playKeyframe('balloon 50000 ease 0 infinite normal forwards');
5-
return $('.sun').playKeyframe('sun 50000 linear 0 infinite normal forwards');
3+
$('.cloud').playKeyframe('clouds 40s linear 0 infinite');
4+
$('.balloon').playKeyframe('balloon 40s ease 0 infinite');
5+
$('.sun').playKeyframe('sun 40s linear 0 infinite');
66
});
77

88
}).call(this);

example/index.html

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
// roll with easing
5555
$('.ball').playKeyframe({
5656
name: 'ball-roll',
57-
duration: 3000,
57+
duration: "3s",
5858
timingFunction: 'ease',
5959
delay: 0,
60-
repeat: 'infinite',
60+
iterationCount: 'infinite',
6161
direction: 'normal',
6262
fillMode: 'forwards'
6363
}, increment);
@@ -66,17 +66,17 @@
6666
case 'spin':
6767

6868
// run spin keyframes using the shorthand method.
69-
$('.ball').playKeyframe('ball-spin 500 linear 0 3 normal forwards', increment);
69+
$('.ball').playKeyframe('ball-spin 500ms linear 0 3 normal forwards', increment);
7070

7171
break;
7272
case 'linear':
7373

7474
// roll with linear timing
7575
$('.ball').playKeyframe({
7676
name: 'ball-roll',
77-
duration: 3000,
77+
duration: "3s",
7878
timingFunction: 'linear',
79-
repeat: 'infinite'
79+
iterationCount: 'infinite'
8080
}, increment);
8181

8282
break;
@@ -85,10 +85,10 @@
8585
// set a 2 second delay before starting
8686
$('.ball').playKeyframe({
8787
name: 'ball-roll',
88-
duration: 3000,
88+
duration: "3s",
8989
timingFunction: 'linear',
90-
delay: 2000,
91-
repeat: 'infinite'
90+
delay: "3s",
91+
iterationCount: 'infinite'
9292
}, increment);
9393

9494
break;
@@ -97,11 +97,22 @@
9797
// only play the animation once
9898
$('.ball').playKeyframe({
9999
name: 'ball-roll',
100-
duration: 3000,
100+
duration: "3s",
101101
timingFunction: 'ease'
102102
}, increment);
103103

104104
break;
105+
case 'multi':
106+
// play multiple animations
107+
$('.ball').playKeyframe([
108+
'ball-spin 500ms linear 0 6',
109+
{
110+
name: 'ball-roll',
111+
duration: "3s",
112+
timingFunction: 'ease',
113+
iterationCount: 1
114+
}
115+
], increment);
105116
}
106117
})
107118

@@ -159,10 +170,11 @@
159170
<input type="button" onclick="play('delay')" value="2 Second Delay" />
160171
<input type="button" onclick="play('once')" value="Roll Once" />
161172
<input type="button" onclick="play('spin')" value="Spin" />
162-
<input type="button" onclick="play()" value="Reset" />
173+
<input type="button" onclick="play('multi')" value="Multiple Animations" />
163174
<br />
164175
<input type="button" onclick="pause()" value="Pause" />
165176
<input type="button" onclick="resume()" value="Resume" />
177+
<input type="button" onclick="play()" value="Reset" />
166178
<br />
167179
<br />Callback count: <b id="cb">0</b>
168180
<br />

jquery.keyframes.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,37 @@
109109

110110
$.fn.playKeyframe = function(frameOptions, callback) {
111111

112-
if (typeof frameOptions === 'string') {
113-
var frameOptSplit = frameOptions.trim().split(' ');
114-
frameOptions = {
115-
name: frameOptSplit[0],
116-
duration: parseInt(frameOptSplit[1]),
117-
timingFunction: frameOptSplit[2],
118-
delay: parseInt(frameOptSplit[3]),
119-
repeat: frameOptSplit[4],
120-
direction: frameOptSplit[5],
121-
fillMode: frameOptSplit[6],
122-
complete: callback
112+
var animObjToStr = function(obj){
113+
obj = $.extend({
114+
duration: 0,
115+
timingFunction: "ease",
116+
delay: 0,
117+
iterationCount: 1,
118+
direction: "normal",
119+
fillMode: "forwards"
120+
}, obj);
121+
return [obj.name, obj.duration, obj.timingFunction, obj.delay, obj.iterationCount, obj.direction, obj.fillMode].join(" ");
122+
};
123+
124+
var animationcss = "";
125+
126+
if($.isArray(frameOptions)){
127+
var frameOptionsStrings = [];
128+
console.log(frameOptions.length);
129+
for(var i = 0; i < frameOptions.length; i++){
130+
if (typeof frameOptions[i] === 'string') {
131+
frameOptionsStrings.push(frameOptions[i]);
132+
}else{
133+
frameOptionsStrings.push(animObjToStr(frameOptions[i]));
134+
}
123135
}
136+
animationcss = frameOptionsStrings.join(", ");
137+
}else if (typeof frameOptions === 'string') {
138+
animationcss = frameOptions;
139+
}else{
140+
animationcss = animObjToStr(frameOptions);
124141
}
125142

126-
frameOptions = $.extend({
127-
duration: 0,
128-
timingFunction: "ease",
129-
delay: 0,
130-
repeat: 1,
131-
direction: "normal",
132-
fillMode: "forwards",
133-
complete: callback
134-
}, frameOptions);
135-
136-
var duration = frameOptions.duration;
137-
var delay = frameOptions.delay;
138-
var repeat = frameOptions.repeat;
139-
var animationcss = "" + frameOptions.name + " " + duration + "ms " + frameOptions.timingFunction + " " + delay + "ms " + repeat + " " + frameOptions.direction + " " + frameOptions.fillMode;
140143
var animationkey = vendorPrefix + "animation";
141144
var pfx = ["webkit", "moz", "MS", "o", ""];
142145

jquery.keyframes.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)