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

· 11년 전 · 3160
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,115
11년 전 조회 3,491
11년 전 조회 2,698
11년 전 조회 3,251
11년 전 조회 749
11년 전 조회 2,516
11년 전 조회 2,489
11년 전 조회 2,564
11년 전 조회 1,551
11년 전 조회 2,028
11년 전 조회 1,347
11년 전 조회 1,156
11년 전 조회 1,743
11년 전 조회 1,062
11년 전 조회 3,948
11년 전 조회 3,723
11년 전 조회 1,358
11년 전 조회 2,595
11년 전 조회 1,005
11년 전 조회 1,819
11년 전 조회 3,426
11년 전 조회 3,729
11년 전 조회 4,647
11년 전 조회 1,045
11년 전 조회 1,602
11년 전 조회 3,011
11년 전 조회 1,183
11년 전 조회 1,180
11년 전 조회 1,789
11년 전 조회 1,057
11년 전 조회 2,323
11년 전 조회 1,833
11년 전 조회 3,913
11년 전 조회 2,366
11년 전 조회 4,615
11년 전 조회 1,391
11년 전 조회 1,254
11년 전 조회 1,897
11년 전 조회 1,863
11년 전 조회 1,443
11년 전 조회 1,083
11년 전 조회 1,712
11년 전 조회 1,100
11년 전 조회 1,208
11년 전 조회 1,413
11년 전 조회 1,231
11년 전 조회 982
11년 전 조회 2,164
11년 전 조회 1,989
11년 전 조회 3,161
11년 전 조회 1,127
11년 전 조회 893
11년 전 조회 990
11년 전 조회 2,864
11년 전 조회 1,116
11년 전 조회 1,312
11년 전 조회 836
11년 전 조회 1,612
11년 전 조회 1,610
11년 전 조회 1,008
11년 전 조회 1,187
11년 전 조회 877
11년 전 조회 831
11년 전 조회 1,658
11년 전 조회 990
11년 전 조회 894
11년 전 조회 1,009
11년 전 조회 1,177
11년 전 조회 850
11년 전 조회 885
11년 전 조회 1,364
11년 전 조회 933
11년 전 조회 1,405
11년 전 조회 925
11년 전 조회 1,048
11년 전 조회 1,106
11년 전 조회 858
11년 전 조회 885
11년 전 조회 1,116
11년 전 조회 2,045
11년 전 조회 890
11년 전 조회 911
11년 전 조회 839
11년 전 조회 1,262
11년 전 조회 910
11년 전 조회 823
11년 전 조회 1,133
11년 전 조회 1,477
11년 전 조회 1,113
11년 전 조회 1,032
11년 전 조회 1,085
11년 전 조회 1,919
11년 전 조회 981
11년 전 조회 1,039
11년 전 조회 2,815
11년 전 조회 2,364
11년 전 조회 1,235
11년 전 조회 1,733
11년 전 조회 1,337
11년 전 조회 1,269
🐛 버그신고