offset() 메서드를 이용하여 left, top 값 set, get 하기

· 11년 전 · 3168
offset()은 요소의  left, top값을 get또는 set 할 수 있는 메서드입니다.
먼저 요소의 left,top값을 get하는 방법을 알아 볼게요
<형식>

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

 <!DOCTYPE HTML>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0;}
        div{width:100px;height:100px;background:red;position:absolute;left:20px;top:50px;}
    </style>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
        $(function(){
            var offset = $('div').offset();
            $('p').html(offset.left + '/' + offset.top);
        });
    </script>
</head>
<body>
    <p></p>
    <div>컨텐츠</div>
</body>
</html>


set하는 형식은
$(선택자).offset({left:값,top:값})

 <!DOCTYPE HTML>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0;}
        div{width:100px;height:100px;background:red;}
    </style>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
        $(function(){
            $('div').offset({left:30,top:50});
        });
    </script>
</head>
<body>   
    <div>컨텐츠</div>
</body>
</html>

|
댓글을 작성하시려면 로그인이 필요합니다. 로그인

프로그램

+
제목 글쓴이 날짜 조회
11년 전 조회 1,122
11년 전 조회 3,504
11년 전 조회 2,708
11년 전 조회 3,267
11년 전 조회 760
11년 전 조회 2,528
11년 전 조회 2,505
11년 전 조회 2,582
11년 전 조회 1,568
11년 전 조회 2,036
11년 전 조회 1,360
11년 전 조회 1,168
11년 전 조회 1,755
11년 전 조회 1,070
11년 전 조회 3,954
11년 전 조회 3,733
11년 전 조회 1,366
11년 전 조회 2,606
11년 전 조회 1,016
11년 전 조회 1,831
11년 전 조회 3,439
11년 전 조회 3,740
11년 전 조회 4,662
11년 전 조회 1,058
11년 전 조회 1,618
11년 전 조회 3,024
11년 전 조회 1,195
11년 전 조회 1,193
11년 전 조회 1,800
11년 전 조회 1,069
11년 전 조회 2,334
11년 전 조회 1,848
11년 전 조회 3,929
11년 전 조회 2,375
11년 전 조회 4,636
11년 전 조회 1,404
11년 전 조회 1,263
11년 전 조회 1,912
11년 전 조회 1,873
11년 전 조회 1,456
11년 전 조회 1,094
11년 전 조회 1,725
11년 전 조회 1,112
11년 전 조회 1,225
11년 전 조회 1,420
11년 전 조회 1,246
11년 전 조회 1,000
11년 전 조회 2,173
11년 전 조회 2,001
11년 전 조회 3,169
11년 전 조회 1,138
11년 전 조회 907
11년 전 조회 1,006
11년 전 조회 2,875
11년 전 조회 1,123
11년 전 조회 1,323
11년 전 조회 844
11년 전 조회 1,626
11년 전 조회 1,617
11년 전 조회 1,023
11년 전 조회 1,196
11년 전 조회 885
11년 전 조회 849
11년 전 조회 1,678
11년 전 조회 1,001
11년 전 조회 905
11년 전 조회 1,022
11년 전 조회 1,189
11년 전 조회 855
11년 전 조회 898
11년 전 조회 1,375
11년 전 조회 945
11년 전 조회 1,416
11년 전 조회 932
11년 전 조회 1,057
11년 전 조회 1,119
11년 전 조회 875
11년 전 조회 899
11년 전 조회 1,129
11년 전 조회 2,056
11년 전 조회 905
11년 전 조회 922
11년 전 조회 855
11년 전 조회 1,275
11년 전 조회 921
11년 전 조회 833
11년 전 조회 1,145
11년 전 조회 1,490
11년 전 조회 1,126
11년 전 조회 1,047
11년 전 조회 1,094
11년 전 조회 1,935
11년 전 조회 996
11년 전 조회 1,049
11년 전 조회 2,829
11년 전 조회 2,382
11년 전 조회 1,244
11년 전 조회 1,741
11년 전 조회 1,350
11년 전 조회 1,283
🐛 버그신고