<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<title>transform:rotate()</title>
<script>
function rotate(obj) {
obj.style.transform = "rotate(90deg)";
}
</script>
</head>
<body>
<h3>클릭하면 90도 회전합니다.</h3>
<hr>
<img src="media/spongebob.png"
width="100" height="100" alt="animation"
onclick="rotate(this)">
</body>
</html>
이 코드에서 처음에 이미지를 클릭시 90도를 회전했으므로 한번더 클릭시 180도를 회전해야하지않나요?
왜 한번밖에 회전이 안되는지 궁금합니다.
obj.style.transform = "rotate(90deg)"; 은 obj 그러니까 이미지의 출력 모습을 정상 모습에서 90도 회전 시킨 모양을 출력하라는 지시입니다.
이 것을 다음과 같이 여러번 부르는 것은
obj.style.transform = "rotate(90deg)"
obj.style.transform = "rotate(90deg)"
obj.style.transform = "rotate(90deg)"
obj.style.transform = "rotate(90deg)"
다음과 같이 변수 n에 3을 여러번 저장하는 것과 같습니다.
n = 3;
n = 3;
n = 3;
n = 3;
그런다고 n이 12가 되는 것은 아닙니다.
즉 obj.style.transform = "rotate(90deg)"은 style을 90도 회전 모습을 정한다라고 지정하는 겁니다.