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

· 11년 전 · 3044
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,579
11년 전 조회 3,149
11년 전 조회 632
11년 전 조회 2,399
11년 전 조회 2,380
11년 전 조회 2,456
11년 전 조회 1,437
11년 전 조회 1,912
11년 전 조회 1,237
11년 전 조회 1,052
11년 전 조회 1,630
11년 전 조회 954
11년 전 조회 3,833
11년 전 조회 3,608
11년 전 조회 1,232
11년 전 조회 2,478
11년 전 조회 901
11년 전 조회 1,704
11년 전 조회 3,309
11년 전 조회 3,614
11년 전 조회 4,537
11년 전 조회 934
11년 전 조회 1,502
11년 전 조회 2,907
11년 전 조회 1,075
11년 전 조회 1,065
11년 전 조회 1,677
11년 전 조회 932
11년 전 조회 2,214
11년 전 조회 1,718
11년 전 조회 3,797
11년 전 조회 2,260
11년 전 조회 4,512
11년 전 조회 1,281
11년 전 조회 1,143
11년 전 조회 1,782
11년 전 조회 1,748
11년 전 조회 1,319
11년 전 조회 969
11년 전 조회 1,622
11년 전 조회 990
11년 전 조회 1,087
11년 전 조회 1,303
11년 전 조회 1,116
11년 전 조회 876
11년 전 조회 2,056
11년 전 조회 1,872
11년 전 조회 3,045
11년 전 조회 1,020
11년 전 조회 782
11년 전 조회 873
11년 전 조회 2,756
11년 전 조회 1,010
11년 전 조회 1,204
11년 전 조회 720
11년 전 조회 1,502
11년 전 조회 1,495
11년 전 조회 903
11년 전 조회 1,089
11년 전 조회 765
11년 전 조회 728
11년 전 조회 1,550
11년 전 조회 887
11년 전 조회 787
11년 전 조회 904
11년 전 조회 1,086
11년 전 조회 737
11년 전 조회 778
11년 전 조회 1,288
11년 전 조회 822
11년 전 조회 1,309
11년 전 조회 820
11년 전 조회 968
11년 전 조회 1,005
11년 전 조회 750
11년 전 조회 784
11년 전 조회 1,019
11년 전 조회 1,955
11년 전 조회 787
11년 전 조회 801
11년 전 조회 741
11년 전 조회 1,155
11년 전 조회 802
11년 전 조회 718
11년 전 조회 1,043
11년 전 조회 1,374
11년 전 조회 1,007
11년 전 조회 929
11년 전 조회 972
11년 전 조회 1,799
11년 전 조회 871
11년 전 조회 930
11년 전 조회 2,709
11년 전 조회 2,260
11년 전 조회 1,138
11년 전 조회 1,629
11년 전 조회 1,238
11년 전 조회 1,166
🐛 버그신고