How to exact set a dynamic style transform using Javascript?
Note: you should use skewX and/or skewY instead of skew. See the MDN info here.
It would be easier to keep track of the transforms in a variable:
var elRot, // the rotation 'counter' for the element 'el'
elScale, // the scale 'counter' for the element 'el'
elSkewX, // the skewX 'counter' for the element 'el'
elSkewY; // the skewY 'counter' for the element 'el'
// initialize values
elRot = 0;
elScale = 1;
elSkewX = 0;
elSkewY = 0;
or an object:
var elTranform = {
rot: 0, // the rotation 'counter' for the element 'el'
sca: 1, // the scale 'counter' for the element 'el'
skx: 0, // the skewX 'counter' for the element 'el'
sky: 0 // the skewY 'counter' for the element 'el'
};
So now you can start with the function (you will still use var el for the element), the first step is getting the values, so you change the arguments given to the function:
// use this with separate vars
function setTransform (element, rotationArg, scaleArg, skewXArg, skewYArg) {
// how to write some code to set the el transform style...
}
// use this with the object
function setTransform (element, elTransformArg) {
// how to write some code to set the el transform style...
}
Next you have re-specify any other transforms that are 'staying', in the example you give, the skewX remains 45deg:
function setTransform (element, rotationArg, scaleArg, skewXArg, skewYArg) {
// the brackets cause the string to be evaluated before being assigned
element.style.transform = ("rotate() scale() skewX() skewY()");
}
or
function setTransform (element, elTransformArg) {
element.style.transform = ("rotate() scale() skewX() skewY()");
}
Now you have to put the arguments into the string:
function setTransform (element, rotationArg, scaleArg, skewXArg, skewYArg) {
element.style.transform = ("rotate(" + rotationArg + "deg ) scale(" + scaleArg
+ ") skewX(" + skewXArg + "deg ) skewY(" + skewYArg + "deg )");
}
or
function setTransform (element, elTransformArg) {
element.style.transform = ("rotate(" + elTransformArg.rot + "deg ) scale("
+ elTransformArg.sca + ") skewX(" + elTransformArg.skx + "deg ) skewY("
+ elTransformArg.sky + "deg )");
}
A bit messy, so put that string in a variable (this will be beneficial for the prefixes):
function setTransform (element, rotationArg, scaleArg, skewXArg, skewYArg) {
var transformString = ("rotate(" + rotationArg + "deg ) scale(" + scaleArg
+ ") skewX(" + skewXArg + "deg ) skewY(" + skewYArg + "deg )");
// now attach that variable to each prefixed style
element.style.webkitTransform = transformString;
element.style.MozTransform = transformString;
element.style.msTransform = transformString;
element.style.OTransform = transformString;
element.style.transform = transformString;
}
or
function setTransform (element, elTransformArg) {
var transformString= ("rotate(" + elTransformArg.rot + "deg ) scale("
+ elTransformArg.sca + ") skewX(" + elTransformArg.skx + "deg ) skewY("
+ elTransformArg.sky + "deg )");
// now attach that variable to each prefixed style
element.style.webkitTransform = transformString;
element.style.MozTransform = transformString;
element.style.msTransform = transformString;
element.style.OTransform = transformString;
element.style.transform = transformString;
}
Now call the function:
setTransform(el, elRot, elScale, elSkewX, elSkewY);
or
setTransform(el, elTransform);
To change values, change the variables or object values and call:
elRot = 45;
elSkewX = 30;
setTransform(el, elRot, elScale, elSkewX, elSkewY);
or
elTransform.rot = 45;
elTransform.skx = 30;
setTransform(el, elTransform);
This way you only have to change the 'counter' values for each transform, and call the function to apply the transform. As mentioned above, it is important to keep any transforms that are not changed as this simply replaces the previous CSS value for the transform with the new one. Giving multiple values means only the last one is used (the C part of CSS).