Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

这种方法为什么无法修改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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

不该是 aStyle.style.transform = 'translateX(50px)'
应该是 a.style.transform = 'translateX(50px)'
然后考虑兼容性,把 webkitTransform 也写上


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...