• 회원가입
  • 로그인
  • 구글아이디로 로그인

[DOM_Element] JS - getBoundingClientRect() 메서드 - 뷰포트 기준 요소크기와 요소위치 반환 (= getBoundingClientRect메서드 = 겟바운딩클라이언트렉트메서드) (IE9 이상)

getBoundingClientRect() 예제

 

<style>

.out {height:200px; width:300px; overflow:auto;}

 #hz {width:250px; height:150px; border:1px solid red;}

.in {width:750px ; height:1000px; background:yellow;}

</style>


<script>

function homzzang() {

  var div = document.getElementById("hz");

  var box = div.getBoundingClientRect();

  x = box.left;

  y = box.top;

  w = box.width;

  h = box.height;

  var res = "Left: " + x + ", Top: " + y + ", Width: " + w + ", Height: " + h;

  alert (res);

}

</script>


<div class="out">

    <div id="hz">홈짱닷컴 Homzzang.com</div>

    <div class="in"></div>

</div>


<button onclick="homzzang()">클릭</button>

 

결과보기

결과값: Left: 8, Top: 8, Width: 260, Height: 160

 

방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
게시물이 없습니다.
목록
찾아주셔서 감사합니다. Since 2012