gpt4 book ai didi

javascript - 如何通过javascript获取值translateX

转载 作者:数据小太阳 更新时间:2023-10-29 03:58:42 26 4
gpt4 key购买 nike

内容元素用 javascript 初始化

content.children[0].style.transform = "translateX(" + (-200) + "px) scaleX(" + 1.6 + ") scaleY(" + 1.2 + ")";

如何获取此元素的 translateX 值?

最佳答案

您可以使用 Window.getComputedStyle()

var myElement = document.querySelector('.hello');
// set inline-style transform to element
myElement.style.transform = "translateX(" + (-200) + "px) scaleX(" + 1.6 + ") scaleY(" + 1.2 + ")";

function getTranslateX() {
var style = window.getComputedStyle(myElement);
var matrix = new WebKitCSSMatrix(style.transform);
console.log('translateX: ', matrix.m41);
}

document.querySelector('button').addEventListener('click', getTranslateX);
.hello {
height: 100px;
width: 100px;
background: orange;
}
<div class="hello"></div>
<button type="button">get value</button>

与上面的示例相同,但不推荐使用:style.webkitTransform

var myElement = document.querySelector('.hello');
myElement.style.transform = "translateX(" + (-200) + "px) scaleX(" + 1.6 + ") scaleY(" + 1.2 + ")";

function getTranslateX() {
var style = window.getComputedStyle(myElement);
var matrix = new WebKitCSSMatrix(style.webkitTransform);
console.log('translateX: ', matrix.m41);
}

document.querySelector('button').addEventListener('click', getTranslateX);
.hello {
height: 100px;
width: 100px;
background: orange;
}
<div class="hello"></div>
<button type="button">get value</button>

如果您想知道为什么要解释 matrix.m41 here

关于javascript - 如何通过javascript获取值translateX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42267189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com