Computer >> 컴퓨터 >  >> 프로그래밍 >> HTML

HTML DOM Style perspectiveOrigin 속성 완벽 정리 – 문법부터 실전 예제까지

HTML DOM Style의 perspectiveOrigin 속성은 HTML 문서 내에서 3D 요소가 x축과 y축 기준으로 어느 지점에 배치될지를 확인(반환)하고 수정할 수 있는 속성입니다. 쉽게 말해, 3D 변형이 적용된 요소를 바라보는 '시점의 원점'을 조절하는 역할을 합니다.

문법(Syntax)

perspectiveOrigin 속성의 기본 문법은 다음과 같습니다.

1. 값 가져오기

object.perspectiveOrigin

2. 값 설정하기

object.perspectiveOrigin = "value"

value 자리에 들어갈 수 있는 값은 아래 표와 같습니다.

설명
initial속성 값을 브라우저의 기본값으로 되돌립니다.
inherit부모 요소로부터 해당 속성 값을 상속받습니다.
x-axis y-axisx-axis는 시점이 x축상에서 위치하는 지점을, y-axis는 시점이 y축상에서 위치하는 지점을 나타냅니다. 예: "20% 50%", "left top", "10px 30px" 등

예제(Example)

다음 예제는 버튼을 클릭하면 빨간 박스의 perspective-origin이 변경되는 코드입니다.

<!DOCTYPE html>
<html>
<style>
    body {
        color: #000;
        height: 100vh;
        background-color: #8BC6EC;
        background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
        perspective: 100px;
    }
    .btn {
        background: #db133a;
        border: none;
        height: 2rem;
        border-radius: 2px;
        width: 40%;
        display: block;
        color: #fff;
        outline: none;
        cursor: pointer;
        margin: 1rem auto;
    }
    .box {
        text-align: center;
        width: 200px;
        height: 200px;
        background-color: #db133a;
        transform: rotateX(25deg);
        margin: 2rem auto;
    }
</style>
<body>
<h1 style="text-align:center">DOM Style perspectiveOrigin Property Demo</h1>
<div class='box'></div>
<button class="btn" onclick="set()">Set PerspectiveOrigin</button>
<script>
    function set() {
        document.body.style.perspectiveOrigin = "20% 50%";
    }
</script>
</body>
</html>

실행 결과(Output)

위 코드를 실행하면 rotateX(25deg)로 약간 기울어진 빨간색 박스와 [Set PerspectiveOrigin] 버튼이 화면에 나타납니다.

버튼을 클릭하면 자바스크립트의 set() 함수가 실행되어 body 요소의 perspectiveOrigin 값이 "20% 50%"로 설정됩니다. 그 결과 3D 시점의 원점이 이동하면서 빨간 박스의 기울어진 모양이 다음과 같이 달라지는 것을 확인할 수 있습니다.

이처럼 perspectiveOrigin 속성을 활용하면 CSS 3D 변형(transform)이 적용된 요소의 원근감 중심을 자유롭게 제어할 수 있어, 입체적인 UI 연출에 유용하게 사용됩니다.