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

· 11년 전 · 3238
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,216
11년 전 조회 3,570
11년 전 조회 2,786
11년 전 조회 3,320
11년 전 조회 851
11년 전 조회 2,606
11년 전 조회 2,570
11년 전 조회 2,641
11년 전 조회 1,642
11년 전 조회 2,114
11년 전 조회 1,444
11년 전 조회 1,227
11년 전 조회 1,844
11년 전 조회 1,166
11년 전 조회 4,030
11년 전 조회 3,818
11년 전 조회 1,442
11년 전 조회 2,679
11년 전 조회 1,104
11년 전 조회 1,899
11년 전 조회 3,504
11년 전 조회 3,812
11년 전 조회 4,734
11년 전 조회 1,145
11년 전 조회 1,673
11년 전 조회 3,083
11년 전 조회 1,277
11년 전 조회 1,278
11년 전 조회 1,867
11년 전 조회 1,163
11년 전 조회 2,410
11년 전 조회 1,905
11년 전 조회 3,982
11년 전 조회 2,438
11년 전 조회 4,697
11년 전 조회 1,503
11년 전 조회 1,338
11년 전 조회 1,985
11년 전 조회 1,951
11년 전 조회 1,531
11년 전 조회 1,164
11년 전 조회 1,821
11년 전 조회 1,200
11년 전 조회 1,293
11년 전 조회 1,514
11년 전 조회 1,325
11년 전 조회 1,061
11년 전 조회 2,276
11년 전 조회 2,069
11년 전 조회 3,239
11년 전 조회 1,218
11년 전 조회 979
11년 전 조회 1,081
11년 전 조회 2,945
11년 전 조회 1,192
11년 전 조회 1,392
11년 전 조회 935
11년 전 조회 1,703
11년 전 조회 1,700
11년 전 조회 1,101
11년 전 조회 1,305
11년 전 조회 965
11년 전 조회 933
11년 전 조회 1,749
11년 전 조회 1,086
11년 전 조회 975
11년 전 조회 1,105
11년 전 조회 1,239
11년 전 조회 948
11년 전 조회 975
11년 전 조회 1,438
11년 전 조회 1,037
11년 전 조회 1,457
11년 전 조회 1,040
11년 전 조회 1,128
11년 전 조회 1,198
11년 전 조회 947
11년 전 조회 972
11년 전 조회 1,201
11년 전 조회 2,112
11년 전 조회 979
11년 전 조회 990
11년 전 조회 924
11년 전 조회 1,378
11년 전 조회 999
11년 전 조회 931
11년 전 조회 1,220
11년 전 조회 1,586
11년 전 조회 1,199
11년 전 조회 1,152
11년 전 조회 1,168
11년 전 조회 2,009
11년 전 조회 1,077
11년 전 조회 1,132
11년 전 조회 2,927
11년 전 조회 2,482
11년 전 조회 1,347
11년 전 조회 1,838
11년 전 조회 1,434
11년 전 조회 1,370
🐛 버그신고