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

· 11년 전 · 4375
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년 전 조회 2,308
11년 전 조회 4,658
11년 전 조회 3,913
11년 전 조회 4,401
11년 전 조회 1,942
11년 전 조회 3,722
11년 전 조회 3,717
11년 전 조회 3,788
11년 전 조회 2,795
11년 전 조회 3,233
11년 전 조회 2,578
11년 전 조회 2,365
11년 전 조회 2,999
11년 전 조회 2,291
11년 전 조회 5,163
11년 전 조회 4,844
11년 전 조회 2,562
11년 전 조회 3,788
11년 전 조회 2,221
11년 전 조회 2,981
11년 전 조회 4,602
11년 전 조회 4,930
11년 전 조회 5,845
11년 전 조회 2,221
11년 전 조회 2,782
11년 전 조회 4,228
11년 전 조회 2,385
11년 전 조회 2,388
11년 전 조회 2,979
11년 전 조회 2,262
11년 전 조회 3,508
11년 전 조회 3,037
11년 전 조회 5,112
11년 전 조회 3,531
11년 전 조회 5,801
11년 전 조회 2,617
11년 전 조회 2,466
11년 전 조회 3,127
11년 전 조회 3,105
11년 전 조회 2,642
11년 전 조회 2,291
11년 전 조회 2,920
11년 전 조회 2,340
11년 전 조회 2,420
11년 전 조회 2,637
11년 전 조회 2,484
11년 전 조회 2,185
11년 전 조회 3,398
11년 전 조회 3,219
11년 전 조회 4,376
11년 전 조회 2,388
11년 전 조회 2,146
11년 전 조회 2,224
11년 전 조회 4,124
11년 전 조회 2,328
11년 전 조회 2,559
11년 전 조회 2,087
11년 전 조회 2,797
11년 전 조회 2,815
11년 전 조회 2,230
11년 전 조회 2,424
11년 전 조회 2,092
11년 전 조회 2,024
11년 전 조회 2,831
11년 전 조회 2,192
11년 전 조회 2,096
11년 전 조회 2,244
11년 전 조회 2,345
11년 전 조회 2,035
11년 전 조회 2,115
11년 전 조회 2,538
11년 전 조회 2,176
11년 전 조회 2,535
11년 전 조회 2,206
11년 전 조회 2,226
11년 전 조회 2,344
11년 전 조회 2,090
11년 전 조회 2,130
11년 전 조회 2,332
11년 전 조회 3,222
11년 전 조회 2,141
11년 전 조회 2,159
11년 전 조회 2,046
11년 전 조회 2,511
11년 전 조회 2,149
11년 전 조회 2,072
11년 전 조회 2,385
11년 전 조회 2,712
11년 전 조회 2,383
11년 전 조회 2,290
11년 전 조회 2,316
11년 전 조회 3,187
11년 전 조회 2,232
11년 전 조회 2,305
11년 전 조회 3,992
11년 전 조회 3,632
11년 전 조회 2,513
11년 전 조회 3,031
11년 전 조회 2,550
11년 전 조회 2,479