这种方法为什么无法修改transform的translate?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#aaa{
width: 100px;height: 100px;
background-color: orange;
transform: translateX(150px);
}
</style>
</head>
<body>
<div id="aaa">
</div>
<script>
let a = document.querySelector('#aaa')
let aStyle = window.getComputedStyle(a)
console.log(aStyle.transform);
function set() {
setTimeout(() => {
aStyle.style.transform= "translateX(50px)";
}, 1000);
}
set()
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不该是
aStyle.style.transform = 'translateX(50px)'
,应该是
a.style.transform = 'translateX(50px)'
吧然后考虑兼容性,把 webkitTransform 也写上
getComputedStyle
is read-only, and should be used to inspect the element's style — including those set by a<style>
element or an external stylesheet._element_.style
object should be used to set styles on that element, or inspect styles directly added to it from JavaScript manipulation or the globalstyle
attribute.getComputedStyle
返回的是只读的element.style
方式更新