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

· 11년 전 · 3094
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,053
11년 전 조회 3,433
11년 전 조회 2,640
11년 전 조회 3,190
11년 전 조회 685
11년 전 조회 2,459
11년 전 조회 2,435
11년 전 조회 2,503
11년 전 조회 1,492
11년 전 조회 1,969
11년 전 조회 1,292
11년 전 조회 1,100
11년 전 조회 1,681
11년 전 조회 1,002
11년 전 조회 3,887
11년 전 조회 3,661
11년 전 조회 1,296
11년 전 조회 2,534
11년 전 조회 944
11년 전 조회 1,757
11년 전 조회 3,361
11년 전 조회 3,665
11년 전 조회 4,587
11년 전 조회 982
11년 전 조회 1,544
11년 전 조회 2,947
11년 전 조회 1,122
11년 전 조회 1,115
11년 전 조회 1,731
11년 전 조회 988
11년 전 조회 2,263
11년 전 조회 1,765
11년 전 조회 3,853
11년 전 조회 2,304
11년 전 조회 4,550
11년 전 조회 1,324
11년 전 조회 1,190
11년 전 조회 1,832
11년 전 조회 1,797
11년 전 조회 1,370
11년 전 조회 1,016
11년 전 조회 1,659
11년 전 조회 1,039
11년 전 조회 1,138
11년 전 조회 1,347
11년 전 조회 1,167
11년 전 조회 917
11년 전 조회 2,102
11년 전 조회 1,922
11년 전 조회 3,094
11년 전 조회 1,073
11년 전 조회 826
11년 전 조회 925
11년 전 조회 2,800
11년 전 조회 1,052
11년 전 조회 1,246
11년 전 조회 767
11년 전 조회 1,547
11년 전 조회 1,532
11년 전 조회 943
11년 전 조회 1,128
11년 전 조회 811
11년 전 조회 770
11년 전 조회 1,595
11년 전 조회 930
11년 전 조회 826
11년 전 조회 940
11년 전 조회 1,111
11년 전 조회 775
11년 전 조회 821
11년 전 조회 1,309
11년 전 조회 864
11년 전 조회 1,334
11년 전 조회 855
11년 전 조회 996
11년 전 조회 1,048
11년 전 조회 793
11년 전 조회 819
11년 전 조회 1,050
11년 전 조회 1,981
11년 전 조회 831
11년 전 조회 841
11년 전 조회 775
11년 전 조회 1,199
11년 전 조회 841
11년 전 조회 757
11년 전 조회 1,071
11년 전 조회 1,411
11년 전 조회 1,044
11년 전 조회 967
11년 전 조회 1,010
11년 전 조회 1,843
11년 전 조회 913
11년 전 조회 970
11년 전 조회 2,743
11년 전 조회 2,294
11년 전 조회 1,168
11년 전 조회 1,663
11년 전 조회 1,272
11년 전 조회 1,205
🐛 버그신고