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

· 11년 전 · 4548
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,455
11년 전 조회 4,817
11년 전 조회 4,084
11년 전 조회 4,563
11년 전 조회 2,118
11년 전 조회 3,904
11년 전 조회 3,874
11년 전 조회 3,941
11년 전 조회 2,957
11년 전 조회 3,397
11년 전 조회 2,740
11년 전 조회 2,521
11년 전 조회 3,165
11년 전 조회 2,466
11년 전 조회 5,332
11년 전 조회 4,916
11년 전 조회 2,734
11년 전 조회 3,948
11년 전 조회 2,385
11년 전 조회 3,143
11년 전 조회 4,788
11년 전 조회 5,113
11년 전 조회 6,021
11년 전 조회 2,389
11년 전 조회 2,961
11년 전 조회 4,389
11년 전 조회 2,560
11년 전 조회 2,554
11년 전 조회 3,139
11년 전 조회 2,454
11년 전 조회 3,685
11년 전 조회 3,199
11년 전 조회 5,294
11년 전 조회 3,696
11년 전 조회 5,988
11년 전 조회 2,812
11년 전 조회 2,612
11년 전 조회 3,298
11년 전 조회 3,272
11년 전 조회 2,808
11년 전 조회 2,455
11년 전 조회 3,108
11년 전 조회 2,503
11년 전 조회 2,597
11년 전 조회 2,820
11년 전 조회 2,643
11년 전 조회 2,355
11년 전 조회 3,582
11년 전 조회 3,368
11년 전 조회 4,549
11년 전 조회 2,551
11년 전 조회 2,301
11년 전 조회 2,402
11년 전 조회 4,290
11년 전 조회 2,494
11년 전 조회 2,747
11년 전 조회 2,283
11년 전 조회 2,976
11년 전 조회 2,989
11년 전 조회 2,395
11년 전 조회 2,604
11년 전 조회 2,284
11년 전 조회 2,230
11년 전 조회 3,004
11년 전 조회 2,391
11년 전 조회 2,289
11년 전 조회 2,442
11년 전 조회 2,499
11년 전 조회 2,229
11년 전 조회 2,298
11년 전 조회 2,710
11년 전 조회 2,389
11년 전 조회 2,712
11년 전 조회 2,397
11년 전 조회 2,404
11년 전 조회 2,529
11년 전 조회 2,288
11년 전 조회 2,338
11년 전 조회 2,496
11년 전 조회 3,394
11년 전 조회 2,352
11년 전 조회 2,357
11년 전 조회 2,238
11년 전 조회 2,700
11년 전 조회 2,357
11년 전 조회 2,233
11년 전 조회 2,590
11년 전 조회 2,892
11년 전 조회 2,568
11년 전 조회 2,487
11년 전 조회 2,498
11년 전 조회 3,366
11년 전 조회 2,409
11년 전 조회 2,478
11년 전 조회 4,053
11년 전 조회 3,811
11년 전 조회 2,682
11년 전 조회 3,210
11년 전 조회 2,738
11년 전 조회 2,674