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

· 11년 전 · 3259
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,225
11년 전 조회 3,590
11년 전 조회 2,807
11년 전 조회 3,337
11년 전 조회 862
11년 전 조회 2,619
11년 전 조회 2,587
11년 전 조회 2,663
11년 전 조회 1,656
11년 전 조회 2,127
11년 전 조회 1,459
11년 전 조회 1,242
11년 전 조회 1,859
11년 전 조회 1,176
11년 전 조회 4,041
11년 전 조회 3,830
11년 전 조회 1,449
11년 전 조회 2,697
11년 전 조회 1,124
11년 전 조회 1,912
11년 전 조회 3,520
11년 전 조회 3,821
11년 전 조회 4,748
11년 전 조회 1,152
11년 전 조회 1,696
11년 전 조회 3,097
11년 전 조회 1,296
11년 전 조회 1,298
11년 전 조회 1,880
11년 전 조회 1,178
11년 전 조회 2,428
11년 전 조회 1,924
11년 전 조회 3,996
11년 전 조회 2,453
11년 전 조회 4,715
11년 전 조회 1,513
11년 전 조회 1,359
11년 전 조회 2,005
11년 전 조회 1,970
11년 전 조회 1,550
11년 전 조회 1,183
11년 전 조회 1,829
11년 전 조회 1,214
11년 전 조회 1,307
11년 전 조회 1,524
11년 전 조회 1,339
11년 전 조회 1,077
11년 전 조회 2,280
11년 전 조회 2,086
11년 전 조회 3,260
11년 전 조회 1,233
11년 전 조회 999
11년 전 조회 1,092
11년 전 조회 2,966
11년 전 조회 1,205
11년 전 조회 1,411
11년 전 조회 951
11년 전 조회 1,716
11년 전 조회 1,715
11년 전 조회 1,116
11년 전 조회 1,320
11년 전 조회 979
11년 전 조회 942
11년 전 조회 1,764
11년 전 조회 1,098
11년 전 조회 996
11년 전 조회 1,125
11년 전 조회 1,251
11년 전 조회 962
11년 전 조회 999
11년 전 조회 1,447
11년 전 조회 1,054
11년 전 조회 1,465
11년 전 조회 1,057
11년 전 조회 1,142
11년 전 조회 1,223
11년 전 조회 964
11년 전 조회 994
11년 전 조회 1,221
11년 전 조회 2,126
11년 전 조회 1,002
11년 전 조회 1,007
11년 전 조회 940
11년 전 조회 1,394
11년 전 조회 1,015
11년 전 조회 952
11년 전 조회 1,241
11년 전 조회 1,599
11년 전 조회 1,220
11년 전 조회 1,170
11년 전 조회 1,187
11년 전 조회 2,026
11년 전 조회 1,091
11년 전 조회 1,147
11년 전 조회 2,946
11년 전 조회 2,501
11년 전 조회 1,361
11년 전 조회 1,859
11년 전 조회 1,451
11년 전 조회 1,381