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

· 11년 전 · 4516
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,431
11년 전 조회 4,794
11년 전 조회 4,053
11년 전 조회 4,537
11년 전 조회 2,084
11년 전 조회 3,863
11년 전 조회 3,845
11년 전 조회 3,907
11년 전 조회 2,918
11년 전 조회 3,368
11년 전 조회 2,714
11년 전 조회 2,498
11년 전 조회 3,125
11년 전 조회 2,430
11년 전 조회 5,301
11년 전 조회 4,897
11년 전 조회 2,697
11년 전 조회 3,920
11년 전 조회 2,361
11년 전 조회 3,117
11년 전 조회 4,763
11년 전 조회 5,082
11년 전 조회 5,996
11년 전 조회 2,353
11년 전 조회 2,926
11년 전 조회 4,362
11년 전 조회 2,534
11년 전 조회 2,526
11년 전 조회 3,104
11년 전 조회 2,410
11년 전 조회 3,658
11년 전 조회 3,172
11년 전 조회 5,259
11년 전 조회 3,667
11년 전 조회 5,958
11년 전 조회 2,773
11년 전 조회 2,582
11년 전 조회 3,263
11년 전 조회 3,239
11년 전 조회 2,779
11년 전 조회 2,420
11년 전 조회 3,071
11년 전 조회 2,472
11년 전 조회 2,554
11년 전 조회 2,788
11년 전 조회 2,610
11년 전 조회 2,321
11년 전 조회 3,547
11년 전 조회 3,342
11년 전 조회 4,517
11년 전 조회 2,516
11년 전 조회 2,281
11년 전 조회 2,369
11년 전 조회 4,256
11년 전 조회 2,464
11년 전 조회 2,714
11년 전 조회 2,235
11년 전 조회 2,936
11년 전 조회 2,950
11년 전 조회 2,369
11년 전 조회 2,572
11년 전 조회 2,246
11년 전 조회 2,194
11년 전 조회 2,973
11년 전 조회 2,363
11년 전 조회 2,248
11년 전 조회 2,404
11년 전 조회 2,461
11년 전 조회 2,194
11년 전 조회 2,271
11년 전 조회 2,676
11년 전 조회 2,353
11년 전 조회 2,670
11년 전 조회 2,355
11년 전 조회 2,375
11년 전 조회 2,496
11년 전 조회 2,256
11년 전 조회 2,306
11년 전 조회 2,461
11년 전 조회 3,361
11년 전 조회 2,312
11년 전 조회 2,316
11년 전 조회 2,199
11년 전 조회 2,664
11년 전 조회 2,318
11년 전 조회 2,209
11년 전 조회 2,554
11년 전 조회 2,850
11년 전 조회 2,533
11년 전 조회 2,438
11년 전 조회 2,466
11년 전 조회 3,337
11년 전 조회 2,370
11년 전 조회 2,439
11년 전 조회 4,045
11년 전 조회 3,775
11년 전 조회 2,656
11년 전 조회 3,176
11년 전 조회 2,703
11년 전 조회 2,635