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

· 11년 전 · 3043
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,000
11년 전 조회 3,380
11년 전 조회 2,578
11년 전 조회 3,149
11년 전 조회 632
11년 전 조회 2,397
11년 전 조회 2,380
11년 전 조회 2,456
11년 전 조회 1,436
11년 전 조회 1,912
11년 전 조회 1,237
11년 전 조회 1,052
11년 전 조회 1,629
11년 전 조회 954
11년 전 조회 3,832
11년 전 조회 3,608
11년 전 조회 1,232
11년 전 조회 2,478
11년 전 조회 901
11년 전 조회 1,703
11년 전 조회 3,309
11년 전 조회 3,614
11년 전 조회 4,537
11년 전 조회 934
11년 전 조회 1,501
11년 전 조회 2,906
11년 전 조회 1,075
11년 전 조회 1,065
11년 전 조회 1,676
11년 전 조회 932
11년 전 조회 2,214
11년 전 조회 1,717
11년 전 조회 3,795
11년 전 조회 2,257
11년 전 조회 4,511
11년 전 조회 1,281
11년 전 조회 1,143
11년 전 조회 1,781
11년 전 조회 1,747
11년 전 조회 1,318
11년 전 조회 969
11년 전 조회 1,622
11년 전 조회 990
11년 전 조회 1,086
11년 전 조회 1,303
11년 전 조회 1,116
11년 전 조회 875
11년 전 조회 2,056
11년 전 조회 1,871
11년 전 조회 3,044
11년 전 조회 1,020
11년 전 조회 781
11년 전 조회 872
11년 전 조회 2,755
11년 전 조회 1,010
11년 전 조회 1,203
11년 전 조회 718
11년 전 조회 1,502
11년 전 조회 1,493
11년 전 조회 902
11년 전 조회 1,088
11년 전 조회 764
11년 전 조회 728
11년 전 조회 1,550
11년 전 조회 887
11년 전 조회 786
11년 전 조회 904
11년 전 조회 1,086
11년 전 조회 736
11년 전 조회 778
11년 전 조회 1,288
11년 전 조회 821
11년 전 조회 1,309
11년 전 조회 820
11년 전 조회 968
11년 전 조회 1,004
11년 전 조회 750
11년 전 조회 783
11년 전 조회 1,018
11년 전 조회 1,955
11년 전 조회 787
11년 전 조회 800
11년 전 조회 740
11년 전 조회 1,155
11년 전 조회 801
11년 전 조회 718
11년 전 조회 1,042
11년 전 조회 1,374
11년 전 조회 1,006
11년 전 조회 929
11년 전 조회 970
11년 전 조회 1,799
11년 전 조회 871
11년 전 조회 929
11년 전 조회 2,708
11년 전 조회 2,259
11년 전 조회 1,138
11년 전 조회 1,628
11년 전 조회 1,237
11년 전 조회 1,166
🐛 버그신고