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

· 11년 전 · 4518
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,433
11년 전 조회 4,795
11년 전 조회 4,061
11년 전 조회 4,542
11년 전 조회 2,088
11년 전 조회 3,870
11년 전 조회 3,849
11년 전 조회 3,912
11년 전 조회 2,923
11년 전 조회 3,370
11년 전 조회 2,718
11년 전 조회 2,500
11년 전 조회 3,128
11년 전 조회 2,437
11년 전 조회 5,306
11년 전 조회 4,900
11년 전 조회 2,703
11년 전 조회 3,923
11년 전 조회 2,362
11년 전 조회 3,120
11년 전 조회 4,767
11년 전 조회 5,086
11년 전 조회 6,001
11년 전 조회 2,358
11년 전 조회 2,934
11년 전 조회 4,367
11년 전 조회 2,538
11년 전 조회 2,529
11년 전 조회 3,110
11년 전 조회 2,416
11년 전 조회 3,661
11년 전 조회 3,175
11년 전 조회 5,262
11년 전 조회 3,673
11년 전 조회 5,959
11년 전 조회 2,780
11년 전 조회 2,587
11년 전 조회 3,272
11년 전 조회 3,246
11년 전 조회 2,786
11년 전 조회 2,422
11년 전 조회 3,075
11년 전 조회 2,476
11년 전 조회 2,562
11년 전 조회 2,791
11년 전 조회 2,613
11년 전 조회 2,331
11년 전 조회 3,553
11년 전 조회 3,347
11년 전 조회 4,519
11년 전 조회 2,520
11년 전 조회 2,285
11년 전 조회 2,374
11년 전 조회 4,263
11년 전 조회 2,472
11년 전 조회 2,721
11년 전 조회 2,244
11년 전 조회 2,943
11년 전 조회 2,956
11년 전 조회 2,376
11년 전 조회 2,577
11년 전 조회 2,251
11년 전 조회 2,199
11년 전 조회 2,977
11년 전 조회 2,364
11년 전 조회 2,256
11년 전 조회 2,411
11년 전 조회 2,466
11년 전 조회 2,200
11년 전 조회 2,274
11년 전 조회 2,684
11년 전 조회 2,357
11년 전 조회 2,678
11년 전 조회 2,364
11년 전 조회 2,379
11년 전 조회 2,500
11년 전 조회 2,261
11년 전 조회 2,310
11년 전 조회 2,470
11년 전 조회 3,370
11년 전 조회 2,318
11년 전 조회 2,318
11년 전 조회 2,203
11년 전 조회 2,669
11년 전 조회 2,328
11년 전 조회 2,214
11년 전 조회 2,561
11년 전 조회 2,856
11년 전 조회 2,542
11년 전 조회 2,442
11년 전 조회 2,473
11년 전 조회 3,338
11년 전 조회 2,377
11년 전 조회 2,444
11년 전 조회 4,047
11년 전 조회 3,783
11년 전 조회 2,660
11년 전 조회 3,182
11년 전 조회 2,706
11년 전 조회 2,640