💻
jQuery
  • 제이쿼리
  • 제이쿼리 기본
  • 선택자
    • 기본 선택자
    • 계층 선택자
    • 속성 선택자
    • 기본 필터 선택자
    • 내용 필터 선택자
    • 보임 필터 선택자
    • 자식 요소 필터 선택자
    • 폼 요소 필터 선택자
  • 탐색
    • find() / filter()
    • each() / $.each()
  • 속성
    • position() / offset()
    • addClass() / removeClass()
    • toggleClass()
    • hasClass()
    • Attr() / removeAttr()
    • scrollTop() / scrollLeft()
  • 변경
    • text() / html()
    • append() / prepend()
    • remove() / empty()
  • 애니메이션
    • show() / hide()
    • fadeIn() / fadeOut()
    • slideUp() / slideDown()
    • animate()
  • 이벤트
Powered by GitBook
On this page

Was this helpful?

  1. 속성

position() / offset()

요소 위치 메서드에는 position()메서드와 offset()메서드가 있습니다. position() 메서드는 포지션 기준이 되는 요소를 기준으로 선택한 요소에서 가로/세로로 떨어진 위치와 좌표값을 반환하거나 변경할 때 사용합니다. offset() 메서드는 문서를 기준으로 선택한 요소의 가로/세로로 떨어진 위치의 좌표값을 반환하거나 변경할 때 사용합니다.

$("선택자").position().left; $("선택자").position().right; $("선택자").position().top; $("선택자").position().bottom;

$("선택자").offset().left; $("선택자").offset().top;

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQueray11 position()/offset()</title>
    <style>
        div {line-height: 40px; text-align: center; background-image: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);}
        .circle1 {position: absolute; left: 100px; top: 400px; width: 40px; height: 40px; border-radius: 50%;}
        .circle2 {position: absolute; right: 100px; bottom: 400px; width: 40px; height: 40px; border-radius: 50%;}
    </style>
</head>
<body>

    <div class="circle1">1</div>
    <div class="circle2">2</div>

    <button class="btn1">circle1</button>
    <button class="btn2">circle2</button>
    <button class="btn3">position1</button>
    <button class="btn4">position2</button>
    <ul>
        <li><span>position().left : </span><span class="pl">0</span></li>
        <li><span>position().top : </span><span class="pt">0</span></li>
        <li><span>position().bottom : </span><span class="pb">0</span></li>
        <li><span>position().right : </span><span class="pr">0</span></li>
        <li><span>offset().left : </span><span class="ol">0</span></li>
        <li><span>offset().top : </span><span class="ot">0</span></li>
        <li><span>width() : </span><span class="wi">0</span></li>
        <li><span>height() : </span><span class="he">0</span></li>
    </ul>

    <!-- script -->
    <script src="jquery.min_1.1.24.js"></script>
    <script>
      $(".btn1").click(function(){
        const pl = $(".circle1").position().left;
        const pt = $(".circle1").position().top;
        const pb = $(".circle1").position().bottom;
        const pr = $(".circle1").position().right;
        const ol = $(".circle1").offset().left;
        const ot = $(".circle1").offset().top;
        const wi = $(".circle1").width();
        const he = $(".circle1").height();

        $(".pl").text(parseInt(pl));
        $(".pt").text(parseInt(pt));
        $(".pb").text(parseInt(pb));
        $(".pr").text(parseInt(pr));
        $(".ol").text(parseInt(ol));
        $(".ot").text(parseInt(ot));
        $(".wi").text(parseInt(wi));
        $(".he").text(parseInt(he));
      });

      $(".btn2").click(function(){
        const pl = $(".circle2").position().left;
        const pt = $(".circle2").position().top;
        const pb = $(".circle2").position().bottom;
        const pr = $(".circle2").position().right;
        const ol = $(".circle2").offset().left;
        const ot = $(".circle2").offset().top;
        const wi = $(".circle2").width();
        const he = $(".circle2").height();

        $(".pl").text(parseInt(pl));
        $(".pt").text(parseInt(pt));
        $(".pb").text(parseInt(pb));
        $(".pr").text(parseInt(pr));
        $(".ol").text(parseInt(ol));
        $(".ot").text(parseInt(ot));
        $(".wi").text(parseInt(wi));
        $(".he").text(parseInt(he));
      });

      $(".btn3").click(function(){
          $(".circle1").offset({top:70, left:200});
      });
      $(".btn4").click(function(){
          $(".circle2").offset({top:270, left:20});
      });
    </script>

</body>
</html>

231p 예제 숙제

자바스크립트에서는 다음과 같이 offsetTop 메서드를 사용한다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>offsetTop</title>
    <style>
        .box1 {
            width: 250px;
            height: 50px;
            background-color: lightseagreen;
            margin-top: 420px;
            border-radius: 100px;
            text-align: center;
            line-height: 50px;
        }

        .box2 {width: 250px;
            height: 50px;
            background-color: #ccc;
            margin-top: 10px;
            border-radius: 100px;
            text-align: center;
            line-height: 50px;}
    </style>
</head>
<body>
    <div class="box1">이 박스의 offsetTop 값은 ? </div>
    <div class="box2"></div>

    <script>
        document.querySelector(".box2").innerText = document.querySelector(".box1").offsetTop;
    </script>
</body>
</html>
Previous속성NextaddClass() / removeClass()

Last updated 4 years ago

Was this helpful?