As commented, I recommend you use an animation library for this, a good example is Velocity JS.
If a library like suggested is not an option, then read on!
If you were looking to do multiple transforms like that in one on top of your previous transforms, then you'd need to use the matrix
value for the transform. Things get a little complicated with the matrix. The parameters for the matrix are in this order: -
matrix(scaleX(),skewY(),skewX(),scaleY(),translateX(),translateY());
Let's use the example of starting off with the initial fish animation you mentioned: -
fish {
animation: fishanimation 4s ease-in-out infinite alternate;
}
@keyframes fishanimation {
0% {
transform: matrix(1, 0, 0, 1, 0, 20);
}
100% {
transform: matrix(1, 0, 0, 1, 0, 80);
}
}
To dynamically change this in JavaScript will require some unpleasant string manipulation and getting the transform property of the element (which is more awkward than you think). Here's an example of how you might do it.
function getTransformValues(obj) {
var transform = {
scaleX: 1,
skewX: 0,
skewY: 0,
scaleY: 1,
translateX: 0,
translateY: 0
};
var matrix = obj.css("-webkit-transform") ||
obj.css("-moz-transform") ||
obj.css("-ms-transform") ||
obj.css("-o-transform") ||
obj.css("transform");
if (matrix && matrix !== 'none') {
var values = matrix.split('(')[1].split(')')[0].split(',');
transform.scaleX = parseFloat(values[0]);
transform.skewY = parseFloat(values[1]);
transform.skewX = parseFloat(values[2]);
transform.scaleY = parseFloat(values[3]);
transform.translateX = parseFloat(values[4]);
transform.translateY = parseFloat(values[5]);
}
return transform;
}
var values = getTransformValues($('#test'));
console.log(values);
// Object {scaleX: 1, skewX: 0, skewY: 0, scaleY: 2, translateX: 50, translateY: 40}
Now you've got those values you can start creating a new matrix by only changing the one you want, for example: -
var old = {
scaleX: 1,
skewX: 0,
skewY: 0,
scaleY: 2,
translateX: 50,
translateY: 40
};
$('#test').css('transform', 'matrix(' + old.scaleX + ', ' + old.skewY + ', ' + old.skewX + ', ' + old.scaleY + ', ' + old.translateX + ', ' + old.translateY + ')');
If you want to start manipulating the rotation angle things get even more complicated. To avoid bogging this answer so much, this part of the question has a solution at the following link: Get element -moz-transform:rotate value in jQuery
Hopefully you can see why adopting libraries such as Velocity JS, at least at the time of writing, is the best way to go for quick, easy, clean and smooth cross-browser animation.