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

· 11년 전 · 4484
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,413
11년 전 조회 4,778
11년 전 조회 4,030
11년 전 조회 4,518
11년 전 조회 2,059
11년 전 조회 3,837
11년 전 조회 3,819
11년 전 조회 3,892
11년 전 조회 2,890
11년 전 조회 3,346
11년 전 조회 2,691
11년 전 조회 2,476
11년 전 조회 3,102
11년 전 조회 2,411
11년 전 조회 5,284
11년 전 조회 4,887
11년 전 조회 2,669
11년 전 조회 3,893
11년 전 조회 2,341
11년 전 조회 3,101
11년 전 조회 4,740
11년 전 조회 5,063
11년 전 조회 5,972
11년 전 조회 2,335
11년 전 조회 2,904
11년 전 조회 4,337
11년 전 조회 2,508
11년 전 조회 2,502
11년 전 조회 3,087
11년 전 조회 2,384
11년 전 조회 3,639
11년 전 조회 3,153
11년 전 조회 5,239
11년 전 조회 3,654
11년 전 조회 5,936
11년 전 조회 2,749
11년 전 조회 2,572
11년 전 조회 3,248
11년 전 조회 3,216
11년 전 조회 2,758
11년 전 조회 2,408
11년 전 조회 3,045
11년 전 조회 2,453
11년 전 조회 2,530
11년 전 조회 2,767
11년 전 조회 2,587
11년 전 조회 2,296
11년 전 조회 3,519
11년 전 조회 3,318
11년 전 조회 4,485
11년 전 조회 2,490
11년 전 조회 2,251
11년 전 조회 2,340
11년 전 조회 4,233
11년 전 조회 2,427
11년 전 조회 2,684
11년 전 조회 2,209
11년 전 조회 2,914
11년 전 조회 2,925
11년 전 조회 2,337
11년 전 조회 2,553
11년 전 조회 2,214
11년 전 조회 2,160
11년 전 조회 2,950
11년 전 조회 2,337
11년 전 조회 2,223
11년 전 조회 2,380
11년 전 조회 2,445
11년 전 조회 2,169
11년 전 조회 2,246
11년 전 조회 2,652
11년 전 조회 2,321
11년 전 조회 2,646
11년 전 조회 2,331
11년 전 조회 2,346
11년 전 조회 2,476
11년 전 조회 2,232
11년 전 조회 2,279
11년 전 조회 2,446
11년 전 조회 3,331
11년 전 조회 2,287
11년 전 조회 2,295
11년 전 조회 2,165
11년 전 조회 2,643
11년 전 조회 2,286
11년 전 조회 2,187
11년 전 조회 2,523
11년 전 조회 2,828
11년 전 조회 2,507
11년 전 조회 2,409
11년 전 조회 2,446
11년 전 조회 3,306
11년 전 조회 2,342
11년 전 조회 2,423
11년 전 조회 4,036
11년 전 조회 3,755
11년 전 조회 2,632
11년 전 조회 3,156
11년 전 조회 2,673
11년 전 조회 2,614