我需要变换旋转值...?警报(“旋转(30度)”)...
<div id="demo" style="transform: rotate(30deg);width:100px;height:50px;background-color:red;">
var myElement = document.getElementById("demo");
var rotate = getElementById("demo").style.transformRotate.value;
alert(rotate);
最佳答案
您可以使用 match
解析 transform
样式获取旋转值:
var myElement = document.getElementById("demo");
var rotate = myElement.style.transform.match(/rotate\((.+)\)/);
alert(rotate && rotate[1]); // avoid error if match returns null
<div id="demo" style="transform: rotate(30deg);width:100px;height:50px;background-color:red;">
要将数字 (30) 与单位 (deg) 分开,您可以执行以下操作:
var myElement = document.getElementById("demo");
var rotate = myElement.style.transform.match(/rotate\((\d+)(.+)\)/);
if (rotate) {
var [num, unit] = rotate.slice(1); // slice is needed since first element contains entire match
console.log('num:', num, 'unit:', unit);
}
<div id="demo" style="transform: rotate(30deg);width:100px;height:50px;background-color:red;">
关于javascript - 如何在javascript中获取样式变换旋转值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59882504/