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

· 11년 전 · 3155
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,113
11년 전 조회 3,486
11년 전 조회 2,693
11년 전 조회 3,245
11년 전 조회 745
11년 전 조회 2,510
11년 전 조회 2,488
11년 전 조회 2,563
11년 전 조회 1,547
11년 전 조회 2,022
11년 전 조회 1,343
11년 전 조회 1,150
11년 전 조회 1,736
11년 전 조회 1,056
11년 전 조회 3,945
11년 전 조회 3,713
11년 전 조회 1,350
11년 전 조회 2,589
11년 전 조회 1,001
11년 전 조회 1,815
11년 전 조회 3,423
11년 전 조회 3,723
11년 전 조회 4,643
11년 전 조회 1,037
11년 전 조회 1,598
11년 전 조회 3,006
11년 전 조회 1,180
11년 전 조회 1,175
11년 전 조회 1,785
11년 전 조회 1,052
11년 전 조회 2,320
11년 전 조회 1,830
11년 전 조회 3,907
11년 전 조회 2,361
11년 전 조회 4,609
11년 전 조회 1,387
11년 전 조회 1,253
11년 전 조회 1,893
11년 전 조회 1,859
11년 전 조회 1,435
11년 전 조회 1,081
11년 전 조회 1,711
11년 전 조회 1,097
11년 전 조회 1,203
11년 전 조회 1,410
11년 전 조회 1,226
11년 전 조회 979
11년 전 조회 2,163
11년 전 조회 1,984
11년 전 조회 3,156
11년 전 조회 1,121
11년 전 조회 891
11년 전 조회 987
11년 전 조회 2,861
11년 전 조회 1,102
11년 전 조회 1,307
11년 전 조회 831
11년 전 조회 1,605
11년 전 조회 1,605
11년 전 조회 1,006
11년 전 조회 1,180
11년 전 조회 871
11년 전 조회 825
11년 전 조회 1,654
11년 전 조회 985
11년 전 조회 890
11년 전 조회 1,005
11년 전 조회 1,173
11년 전 조회 843
11년 전 조회 881
11년 전 조회 1,358
11년 전 조회 929
11년 전 조회 1,402
11년 전 조회 919
11년 전 조회 1,046
11년 전 조회 1,102
11년 전 조회 855
11년 전 조회 880
11년 전 조회 1,106
11년 전 조회 2,040
11년 전 조회 883
11년 전 조회 906
11년 전 조회 833
11년 전 조회 1,259
11년 전 조회 908
11년 전 조회 817
11년 전 조회 1,125
11년 전 조회 1,473
11년 전 조회 1,107
11년 전 조회 1,026
11년 전 조회 1,081
11년 전 조회 1,912
11년 전 조회 975
11년 전 조회 1,033
11년 전 조회 2,809
11년 전 조회 2,360
11년 전 조회 1,231
11년 전 조회 1,726
11년 전 조회 1,331
11년 전 조회 1,265
🐛 버그신고