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

· 11년 전 · 4435
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,371
11년 전 조회 4,735
11년 전 조회 3,992
11년 전 조회 4,475
11년 전 조회 2,012
11년 전 조회 3,793
11년 전 조회 3,775
11년 전 조회 3,851
11년 전 조회 2,843
11년 전 조회 3,301
11년 전 조회 2,649
11년 전 조회 2,434
11년 전 조회 3,062
11년 전 조회 2,360
11년 전 조회 5,238
11년 전 조회 4,868
11년 전 조회 2,620
11년 전 조회 3,855
11년 전 조회 2,292
11년 전 조회 3,057
11년 전 조회 4,686
11년 전 조회 5,007
11년 전 조회 5,912
11년 전 조회 2,285
11년 전 조회 2,849
11년 전 조회 4,286
11년 전 조회 2,460
11년 전 조회 2,455
11년 전 조회 3,042
11년 전 조회 2,325
11년 전 조회 3,585
11년 전 조회 3,097
11년 전 조회 5,186
11년 전 조회 3,603
11년 전 조회 5,874
11년 전 조회 2,696
11년 전 조회 2,519
11년 전 조회 3,201
11년 전 조회 3,162
11년 전 조회 2,705
11년 전 조회 2,360
11년 전 조회 2,993
11년 전 조회 2,406
11년 전 조회 2,479
11년 전 조회 2,716
11년 전 조회 2,551
11년 전 조회 2,242
11년 전 조회 3,470
11년 전 조회 3,275
11년 전 조회 4,436
11년 전 조회 2,437
11년 전 조회 2,203
11년 전 조회 2,284
11년 전 조회 4,179
11년 전 조회 2,381
11년 전 조회 2,631
11년 전 조회 2,149
11년 전 조회 2,857
11년 전 조회 2,876
11년 전 조회 2,295
11년 전 조회 2,501
11년 전 조회 2,161
11년 전 조회 2,096
11년 전 조회 2,894
11년 전 조회 2,280
11년 전 조회 2,164
11년 전 조회 2,333
11년 전 조회 2,406
11년 전 조회 2,111
11년 전 조회 2,197
11년 전 조회 2,599
11년 전 조회 2,258
11년 전 조회 2,591
11년 전 조회 2,293
11년 전 조회 2,292
11년 전 조회 2,416
11년 전 조회 2,175
11년 전 조회 2,203
11년 전 조회 2,399
11년 전 조회 3,275
11년 전 조회 2,223
11년 전 조회 2,234
11년 전 조회 2,107
11년 전 조회 2,578
11년 전 조회 2,224
11년 전 조회 2,136
11년 전 조회 2,460
11년 전 조회 2,779
11년 전 조회 2,446
11년 전 조회 2,362
11년 전 조회 2,388
11년 전 조회 3,253
11년 전 조회 2,291
11년 전 조회 2,368
11년 전 조회 4,017
11년 전 조회 3,693
11년 전 조회 2,571
11년 전 조회 3,105
11년 전 조회 2,615
11년 전 조회 2,553