javascript - 如何用jQuery选择CSS3矩阵变换的子属性

标签 javascript jquery css transform css-transforms

我的 .test 类如下所示:

.test {
    transform-style: preserve-3d;
    width: 460px;
    height: 738px;
    transform: translate3d(0px, 0px, 480.89px)
    matrix3d(-0.959423, 0.0701992, -0.273093, 0, 0, -0.968514, -0.248959, 0, 0.281971, 0.238857, -0.929215, 0, 0, 0, 0, 1)
    translate3d(230px, 369px, 0px);
}

由于 matrix3d 是动态的,我正在尝试监听 matrix3d第三个 数组成员的变化并运行条件 if 语句在上面。如何做到这一点?

最佳答案

您可以使用 getComputedStyle 获取 transform 属性的值,然后拆分它(或使用 RegEx,无论您喜欢哪个)以获得单独的值,如下面的片段。

var el = document.querySelector('.test'),
  op = document.querySelector('output'),
  btn = document.querySelector('.change'),
  oldparams, newparams;

window.onload = function() {

  /* get matrix on load */
  oldparams = getMatrix();
  op.innerHTML += 'Original Matrix Row 3: ' + oldparams[8] + ',' + oldparams[9] + ',' + oldparams[10] + ',' + oldparams[11]; /* since array is 0 based, 8 - 11 is the 3rd row. */

  /* on click of button, change matrix and get the changed value */
  btn.addEventListener('click', function() {
    el.style.transform = 'translate3d(0px, 0px, 480.89px) matrix3d(-0.959423, 0.0701992, -0.273093, 0, 0, -0.968514, -0.248959, 0, 0.383571, 0.578857, -0.639215, 0, 0, 0, 0, 1) translate3d(230px, 369px, 0px)';
    newparams = getMatrix();
    op.innerHTML += '<br><br> New Matrix Row 3: ' + newparams[8] + ', ' + newparams[9] + ', ' + newparams[10] + ', ' + newparams[11]; /* since array is 0 based, 8 - 11 is the 3rd row. */
  });
}

function getMatrix() {
  var matrix = window.getComputedStyle(el).transform;
  /*op.innerHTML = 'Matrix Transform is:' + matrix; */

  var params = matrix.split('(')[1].split(')')[0].split(',');
  return params;
}
.test {
  transform-style: preserve-3d;
  width: 460px;
  height: 738px;
  transform: translate3d(0px, 0px, 480.89px) matrix3d(-0.959423, 0.0701992, -0.273093, 0, 0, -0.968514, -0.248959, 0, 0.281971, 0.238857, -0.929215, 0, 0, 0, 0, 1) translate3d(230px, 369px, 0px);
}
<h2>Output</h2>
<output></output>
<button class='change'>Change Matrix</button>
<div class='test'>Some text</div>

关于javascript - 如何用jQuery选择CSS3矩阵变换的子属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37496111/

相关文章:

javascript - 从基于哈希的导航切换到 HTML5 History API

javascript - react native 文本不环绕在 native 基本标题上

javascript - jqueryUI 自动完成示例如何摆脱不使用 $(document).ready 的情况

javascript - html 或 js - 在页面中创建大量类似的 html 标签

javascript - 在 IE 中找不到 jQuery Slideshow 的交叉签名图像

javascript - 为什么我的条件总是正确的

javascript - 根据另一个数组的顺序对数组进行排序

css - 填充外部导入的 svg 文件

javascript - CSS动画汉堡包图标并删除滚动类?

php - JQuery 帖子不起作用问题