🖋️
JQuery
  • 제이쿼리
  • 제이쿼리 기본
  • 선택자
    • 기본 선택자
    • 계층 선택자
    • 속성 선택자
    • 기본 필터 선택자
    • 내용 필터 선택자
    • 보임 필터 선택자
    • 자식 요소 필터 선택자
    • 폼 요소 필터 선택자
  • 탐색
    • .find()/filter()
    • each()/$.each()
  • 속성
    • addClass() / removeClass()
    • toggleClass()
    • hasClass()
    • attr()/removeAttr()
    • position()/offset()
    • 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() 메서드는 문서를 기준으로 선택한 요소의 가로/세로로 떨어진 위치에 좌표값을 반환하거나 변경할 때 사용합니다.

Previousattr()/removeAttr()NextscrollTop()/scrollLeft()

Last updated 4 years ago

Was this helpful?

$("선택자").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>JQuery</title>
    <style>
    div {
        line-height: 40px; text-align: center;
        background-image: linear-gradient(to top, #ff0844 0%, #ffb199 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.12.4.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(pl);
            $(".pt").text(pt);
            $(".pb").text(pb);
            $(".pr").text(pr);
            $(".ol").text(ol);
            $(".ot").text(ot);
            $(".wi").text(wi);
            $(".he").text(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(pl);
            $(".pt").text(pt);
            $(".pb").text(pb);
            $(".pr").text(pr);
            $(".ol").text(ol);
            $(".ot").text(ot);
            $(".wi").text(wi);
            $(".he").text(he);
        });
        
        $(".btn3").click(function(){
            $(".circle1").offset({top:70, left:200});
        });
        $(".btn4").click(function(){
            $(".circle2").offset({top:270, left:20});
        });
    </script>
</body>
</html>


position().left :100
position().top :400
position().bottom :0
position().right :0
offset().left :100
offset().top:400
width():40
height():40

position().left :1780
position().top :529
position().bottom :0
position().right :0
offset().left :1780
offset().top:529
width():40
height():40
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JQuery</title>
    <style>
        *{margin: 0; padding: 0;}
        #box_wrap{
            width: 300px;
            height: 200px;
            margin: 50px auto 0;
            position: relative;
            background-color: #ccc;
        }
        .box{
            width: 50px; height: 50px;
            position: absolute; left: 100px; top: 50px;
            background-color: #f00;
        }
    </style>
</head>
<body>
    <div id="box_wrap">
        <p class="box">박스</p>
    </div>
        <p class="txt_1">절대 top위칫값: <span></span></p>
        <p class="txt_2">상대 top위칫값: <span></span></p>

    <!-- script -->
    <script src="jquery.min_1.12.4.js"></script>
    <script>
        $(function(){
            let $txt1 = $(".txt_1 span");
            let $txt2 = $(".txt_2 span");
            let $box = $("box");

            let off_t = $box.offset().top;   // 100
            let pos_t = $box.position().top; // 50

            $txt1.text(off_t);
            $txt2.text(pos_t);
        });
    </script>
</body>
</html>