如何使用Javascript精确设置动态样式转换?

时间:2013-10-30 07:13:25

标签: javascript css css3 transform css-transforms

我知道设置变换的方法,但我想设置旋转并保留其他像缩放和倾斜。

也许,我们可以写一个函数:

// A variable of element, not parameter. separate out it make more clear.
var el = document.getElementById('el');
function setTransform (p_name, p_value) {
  // how to write some code to set the el transform style...
}

此外,我有一个元素:

<div id="el" style="transform: rotate(45deg) skew(45deg);"></div>

我们的函数setTransform:

setTransform('rotate', '30deg');

我希望元素是这样的:

<div id="el" style="transform: rotate(30deg) skew(45deg);"></div>

所以我的问题是如何编写函数setTransform的内容?

3 个答案:

答案 0 :(得分:15)

注意:您应该使用skewX和/或skewY而不是skew。请参阅the MDN info here

跟踪变量中的变换会更容易:

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;

或对象:

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'
};

所以现在你可以从函数开始(你仍然会使用var el作为元素),第一步是获取值,所以你改变了给函数的参数:

// 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...
}

接下来,您重新指定了任何其他“保留”的变换,在您给出的示例中,skewX仍为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()");
}

function setTransform (element, elTransformArg) {
    element.style.transform = ("rotate() scale() skewX() skewY()");
}

现在你必须将参数放入字符串中:

function setTransform (element, rotationArg, scaleArg, skewXArg, skewYArg) {
    element.style.transform = ("rotate(" + rotationArg + "deg ) scale(" + scaleArg
        + ") skewX(" + skewXArg + "deg ) skewY(" + skewYArg + "deg )");
}

function setTransform (element, elTransformArg) {
    element.style.transform = ("rotate(" + elTransformArg.rot + "deg ) scale("
        + elTransformArg.sca + ") skewX(" + elTransformArg.skx + "deg ) skewY("
        + elTransformArg.sky + "deg )");
}

有点乱,所以把那个字符串放在一个变量中(这对前缀有用):

function setTransform (element, rotationArg, scaleArg, skewXArg, skewYArg) {
    var transfromString = ("rotate(" + rotationArg + "deg ) scale(" + scaleArg
        + ") skewX(" + skewXArg + "deg ) skewY(" + skewYArg + "deg )");

    // now attach that variable to each prefixed style
    element.style.webkitTransform = transfromString;
    element.style.MozTransform = transfromString;
    element.style.msTransform = transfromString;
    element.style.OTransform = transfromString;
    element.style.transform = transfromString;
}

function setTransform (element, elTransformArg) {
    var transfromString = ("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 = transfromString;
    element.style.MozTransform = transfromString;
    element.style.msTransform = transfromString;
    element.style.OTransform = transfromString;
    element.style.transform = transfromString;
}

现在调用函数:

setTransform(el, elRot, elScale, elSkewX, elSkewY);

setTransform(el, elTransform);

要更改值,请更改变量或对象值并调用:

elRot = 45;
elSkewX = 30;
setTransform(el, elRot, elScale, elSkewX, elSkewY);

elTransform.rot = 45;
elTransform.skx = 30;
setTransform(el, elTransform);

这样,您只需更改每个变换的“计数器”值,并调用该函数以应用变换。如上所述,保持任何未更改的转换非常重要,因为这只是用新的转换替换先前的转换CSS值。赋予多个值意味着只使用最后一个值(CSS的C部分)。

答案 1 :(得分:1)

可能会有以下帮助...

el.style.webkitTransform = "";
el.style.MozTransform = "";
el.style.msTransform = "";
el.style.OTransform = "";
el.style.transform = "";

答案 2 :(得分:1)

这里绝对最好的答案是一个真正的API,它允许我们影响计算样式的任何现有值,而不必解析值。这样的事情存在于WebkitCSSMatrix API和W3C“官方”等同DOMMatrix的一些放弃和非跨浏览的形式中。我已经看到了重新格式化和整合这两个API的讨论,但他们似乎并不同意实现。这是一个让它跨浏览器工作的垫片:CSSMatrix class from github user arian

这是如何工作的,你可以传递计算的变换字符串(例如:"translsate3d(0px, 0px, 100px) scale(1.2) skew(2)"),它返回一个可以用旋转,变换,平移,倾斜函数操作的对象。每个函数都使用新操作的值返回自身的现有实例,您只需将其“toString”值重新应用于元素样式属性即可。

很多词,但这里有一个例子(Chrome和Safari只是因为WebkitCSSMatrix的使用):

var computedStyle = window.getComputedStyle(document.querySelector('.btn'));
var matrix = new WebKitCSSMatrix(computedStyle.transform);

// Every second, we add +6 degrees of rotation, no need to read the initial value since WebkitCSSMatrix has parsed it for us
window.setInterval(function(){
  matrix = matrix.rotate(6);
  // matrix = matrix.translate(2, 0, 0);
  // matrix = matrix.scale(1.8);
  document.querySelector('.btn').style.transform = matrix.toString();
}, 1000);
body{
			padding-top: 100px;
			text-align: center;
		}

		.btn{
			text-transform: uppercase;
			border-radius:200%;
			border:1px solid #000;
			width: 2em;
			height: 2em;
			display: inline-block;
			text-align: center;
			line-height: 2em;
			text-decoration: none;
			font-family: sans-serif;
			color: #000;
      
      // It is already rotated and scaled
			transition: transform 1000ms cubic-bezier(.28,.73,.31,1);
      transform: rotate(45deg);
      -webkit-transform: rotate(45deg);
		}

		.btn:hover{
			background:#000;
			color:#fff;
      
      transform: rotate(0deg) scale(1.5);
		}
<a class="btn" href="#">|</a>