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

· 11년 전 · 3159
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,114
11년 전 조회 3,489
11년 전 조회 2,697
11년 전 조회 3,249
11년 전 조회 749
11년 전 조회 2,513
11년 전 조회 2,489
11년 전 조회 2,564
11년 전 조회 1,550
11년 전 조회 2,025
11년 전 조회 1,346
11년 전 조회 1,153
11년 전 조회 1,740
11년 전 조회 1,061
11년 전 조회 3,947
11년 전 조회 3,718
11년 전 조회 1,355
11년 전 조회 2,594
11년 전 조회 1,004
11년 전 조회 1,818
11년 전 조회 3,423
11년 전 조회 3,728
11년 전 조회 4,644
11년 전 조회 1,041
11년 전 조회 1,601
11년 전 조회 3,008
11년 전 조회 1,182
11년 전 조회 1,179
11년 전 조회 1,785
11년 전 조회 1,056
11년 전 조회 2,322
11년 전 조회 1,831
11년 전 조회 3,910
11년 전 조회 2,366
11년 전 조회 4,614
11년 전 조회 1,390
11년 전 조회 1,254
11년 전 조회 1,896
11년 전 조회 1,862
11년 전 조회 1,441
11년 전 조회 1,082
11년 전 조회 1,712
11년 전 조회 1,100
11년 전 조회 1,207
11년 전 조회 1,412
11년 전 조회 1,229
11년 전 조회 981
11년 전 조회 2,164
11년 전 조회 1,987
11년 전 조회 3,159
11년 전 조회 1,125
11년 전 조회 892
11년 전 조회 990
11년 전 조회 2,863
11년 전 조회 1,112
11년 전 조회 1,311
11년 전 조회 836
11년 전 조회 1,610
11년 전 조회 1,608
11년 전 조회 1,008
11년 전 조회 1,186
11년 전 조회 875
11년 전 조회 831
11년 전 조회 1,657
11년 전 조회 989
11년 전 조회 893
11년 전 조회 1,008
11년 전 조회 1,175
11년 전 조회 848
11년 전 조회 883
11년 전 조회 1,363
11년 전 조회 933
11년 전 조회 1,404
11년 전 조회 924
11년 전 조회 1,047
11년 전 조회 1,104
11년 전 조회 858
11년 전 조회 884
11년 전 조회 1,113
11년 전 조회 2,045
11년 전 조회 887
11년 전 조회 910
11년 전 조회 839
11년 전 조회 1,260
11년 전 조회 910
11년 전 조회 821
11년 전 조회 1,132
11년 전 조회 1,477
11년 전 조회 1,110
11년 전 조회 1,030
11년 전 조회 1,084
11년 전 조회 1,917
11년 전 조회 979
11년 전 조회 1,038
11년 전 조회 2,813
11년 전 조회 2,363
11년 전 조회 1,235
11년 전 조회 1,731
11년 전 조회 1,334
11년 전 조회 1,269
🐛 버그신고