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

· 11년 전 · 3042
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년 전 조회 998
11년 전 조회 3,380
11년 전 조회 2,578
11년 전 조회 3,149
11년 전 조회 631
11년 전 조회 2,397
11년 전 조회 2,380
11년 전 조회 2,456
11년 전 조회 1,436
11년 전 조회 1,912
11년 전 조회 1,235
11년 전 조회 1,051
11년 전 조회 1,627
11년 전 조회 954
11년 전 조회 3,831
11년 전 조회 3,607
11년 전 조회 1,232
11년 전 조회 2,477
11년 전 조회 898
11년 전 조회 1,702
11년 전 조회 3,309
11년 전 조회 3,613
11년 전 조회 4,537
11년 전 조회 932
11년 전 조회 1,501
11년 전 조회 2,906
11년 전 조회 1,072
11년 전 조회 1,060
11년 전 조회 1,676
11년 전 조회 929
11년 전 조회 2,213
11년 전 조회 1,717
11년 전 조회 3,795
11년 전 조회 2,257
11년 전 조회 4,511
11년 전 조회 1,280
11년 전 조회 1,142
11년 전 조회 1,781
11년 전 조회 1,747
11년 전 조회 1,317
11년 전 조회 969
11년 전 조회 1,619
11년 전 조회 990
11년 전 조회 1,086
11년 전 조회 1,302
11년 전 조회 1,113
11년 전 조회 874
11년 전 조회 2,055
11년 전 조회 1,870
11년 전 조회 3,043
11년 전 조회 1,020
11년 전 조회 781
11년 전 조회 870
11년 전 조회 2,755
11년 전 조회 1,009
11년 전 조회 1,203
11년 전 조회 717
11년 전 조회 1,500
11년 전 조회 1,492
11년 전 조회 900
11년 전 조회 1,086
11년 전 조회 764
11년 전 조회 726
11년 전 조회 1,549
11년 전 조회 887
11년 전 조회 786
11년 전 조회 904
11년 전 조회 1,086
11년 전 조회 735
11년 전 조회 777
11년 전 조회 1,288
11년 전 조회 821
11년 전 조회 1,308
11년 전 조회 819
11년 전 조회 968
11년 전 조회 1,004
11년 전 조회 749
11년 전 조회 783
11년 전 조회 1,018
11년 전 조회 1,955
11년 전 조회 787
11년 전 조회 800
11년 전 조회 739
11년 전 조회 1,155
11년 전 조회 801
11년 전 조회 718
11년 전 조회 1,042
11년 전 조회 1,373
11년 전 조회 1,006
11년 전 조회 928
11년 전 조회 970
11년 전 조회 1,799
11년 전 조회 870
11년 전 조회 929
11년 전 조회 2,707
11년 전 조회 2,258
11년 전 조회 1,138
11년 전 조회 1,628
11년 전 조회 1,237
11년 전 조회 1,166
🐛 버그신고