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

· 11년 전 · 3239
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,219
11년 전 조회 3,571
11년 전 조회 2,788
11년 전 조회 3,320
11년 전 조회 853
11년 전 조회 2,608
11년 전 조회 2,572
11년 전 조회 2,643
11년 전 조회 1,643
11년 전 조회 2,117
11년 전 조회 1,446
11년 전 조회 1,229
11년 전 조회 1,846
11년 전 조회 1,166
11년 전 조회 4,032
11년 전 조회 3,818
11년 전 조회 1,442
11년 전 조회 2,680
11년 전 조회 1,105
11년 전 조회 1,899
11년 전 조회 3,506
11년 전 조회 3,813
11년 전 조회 4,734
11년 전 조회 1,145
11년 전 조회 1,675
11년 전 조회 3,085
11년 전 조회 1,282
11년 전 조회 1,279
11년 전 조회 1,868
11년 전 조회 1,164
11년 전 조회 2,412
11년 전 조회 1,907
11년 전 조회 3,982
11년 전 조회 2,438
11년 전 조회 4,698
11년 전 조회 1,507
11년 전 조회 1,340
11년 전 조회 1,985
11년 전 조회 1,952
11년 전 조회 1,532
11년 전 조회 1,168
11년 전 조회 1,824
11년 전 조회 1,201
11년 전 조회 1,293
11년 전 조회 1,519
11년 전 조회 1,328
11년 전 조회 1,061
11년 전 조회 2,277
11년 전 조회 2,072
11년 전 조회 3,240
11년 전 조회 1,221
11년 전 조회 981
11년 전 조회 1,085
11년 전 조회 2,947
11년 전 조회 1,193
11년 전 조회 1,392
11년 전 조회 936
11년 전 조회 1,704
11년 전 조회 1,704
11년 전 조회 1,103
11년 전 조회 1,311
11년 전 조회 966
11년 전 조회 934
11년 전 조회 1,750
11년 전 조회 1,086
11년 전 조회 976
11년 전 조회 1,106
11년 전 조회 1,241
11년 전 조회 949
11년 전 조회 978
11년 전 조회 1,438
11년 전 조회 1,039
11년 전 조회 1,458
11년 전 조회 1,046
11년 전 조회 1,129
11년 전 조회 1,198
11년 전 조회 949
11년 전 조회 974
11년 전 조회 1,202
11년 전 조회 2,113
11년 전 조회 982
11년 전 조회 991
11년 전 조회 925
11년 전 조회 1,379
11년 전 조회 1,000
11년 전 조회 935
11년 전 조회 1,222
11년 전 조회 1,589
11년 전 조회 1,199
11년 전 조회 1,157
11년 전 조회 1,171
11년 전 조회 2,009
11년 전 조회 1,078
11년 전 조회 1,133
11년 전 조회 2,930
11년 전 조회 2,482
11년 전 조회 1,347
11년 전 조회 1,847
11년 전 조회 1,435
11년 전 조회 1,372
🐛 버그신고