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

· 11년 전 · 3052
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,012
11년 전 조회 3,392
11년 전 조회 2,594
11년 전 조회 3,159
11년 전 조회 647
11년 전 조회 2,409
11년 전 조회 2,391
11년 전 조회 2,466
11년 전 조회 1,449
11년 전 조회 1,920
11년 전 조회 1,250
11년 전 조회 1,060
11년 전 조회 1,641
11년 전 조회 970
11년 전 조회 3,843
11년 전 조회 3,620
11년 전 조회 1,247
11년 전 조회 2,493
11년 전 조회 911
11년 전 조회 1,717
11년 전 조회 3,323
11년 전 조회 3,624
11년 전 조회 4,547
11년 전 조회 946
11년 전 조회 1,508
11년 전 조회 2,912
11년 전 조회 1,088
11년 전 조회 1,076
11년 전 조회 1,687
11년 전 조회 945
11년 전 조회 2,225
11년 전 조회 1,728
11년 전 조회 3,807
11년 전 조회 2,267
11년 전 조회 4,515
11년 전 조회 1,294
11년 전 조회 1,152
11년 전 조회 1,795
11년 전 조회 1,759
11년 전 조회 1,334
11년 전 조회 979
11년 전 조회 1,632
11년 전 조회 1,004
11년 전 조회 1,099
11년 전 조회 1,315
11년 전 조회 1,128
11년 전 조회 884
11년 전 조회 2,068
11년 전 조회 1,883
11년 전 조회 3,053
11년 전 조회 1,031
11년 전 조회 794
11년 전 조회 886
11년 전 조회 2,766
11년 전 조회 1,020
11년 전 조회 1,215
11년 전 조회 733
11년 전 조회 1,514
11년 전 조회 1,505
11년 전 조회 915
11년 전 조회 1,101
11년 전 조회 779
11년 전 조회 739
11년 전 조회 1,563
11년 전 조회 897
11년 전 조회 800
11년 전 조회 912
11년 전 조회 1,091
11년 전 조회 746
11년 전 조회 792
11년 전 조회 1,292
11년 전 조회 834
11년 전 조회 1,314
11년 전 조회 833
11년 전 조회 973
11년 전 조회 1,018
11년 전 조회 762
11년 전 조회 790
11년 전 조회 1,027
11년 전 조회 1,958
11년 전 조회 799
11년 전 조회 810
11년 전 조회 745
11년 전 조회 1,172
11년 전 조회 807
11년 전 조회 728
11년 전 조회 1,049
11년 전 조회 1,384
11년 전 조회 1,015
11년 전 조회 944
11년 전 조회 978
11년 전 조회 1,812
11년 전 조회 880
11년 전 조회 938
11년 전 조회 2,719
11년 전 조회 2,268
11년 전 조회 1,144
11년 전 조회 1,638
11년 전 조회 1,243
11년 전 조회 1,176
🐛 버그신고