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

· 11년 전 · 4507
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,425
11년 전 조회 4,791
11년 전 조회 4,047
11년 전 조회 4,534
11년 전 조회 2,079
11년 전 조회 3,855
11년 전 조회 3,839
11년 전 조회 3,903
11년 전 조회 2,915
11년 전 조회 3,365
11년 전 조회 2,708
11년 전 조회 2,494
11년 전 조회 3,122
11년 전 조회 2,427
11년 전 조회 5,292
11년 전 조회 4,895
11년 전 조회 2,692
11년 전 조회 3,914
11년 전 조회 2,354
11년 전 조회 3,114
11년 전 조회 4,760
11년 전 조회 5,078
11년 전 조회 5,988
11년 전 조회 2,349
11년 전 조회 2,922
11년 전 조회 4,350
11년 전 조회 2,528
11년 전 조회 2,519
11년 전 조회 3,100
11년 전 조회 2,400
11년 전 조회 3,654
11년 전 조회 3,169
11년 전 조회 5,255
11년 전 조회 3,660
11년 전 조회 5,953
11년 전 조회 2,767
11년 전 조회 2,579
11년 전 조회 3,256
11년 전 조회 3,237
11년 전 조회 2,773
11년 전 조회 2,416
11년 전 조회 3,061
11년 전 조회 2,467
11년 전 조회 2,546
11년 전 조회 2,782
11년 전 조회 2,603
11년 전 조회 2,315
11년 전 조회 3,537
11년 전 조회 3,336
11년 전 조회 4,508
11년 전 조회 2,510
11년 전 조회 2,273
11년 전 조회 2,358
11년 전 조회 4,253
11년 전 조회 2,457
11년 전 조회 2,707
11년 전 조회 2,227
11년 전 조회 2,930
11년 전 조회 2,945
11년 전 조회 2,362
11년 전 조회 2,567
11년 전 조회 2,238
11년 전 조회 2,188
11년 전 조회 2,968
11년 전 조회 2,354
11년 전 조회 2,241
11년 전 조회 2,394
11년 전 조회 2,458
11년 전 조회 2,188
11년 전 조회 2,261
11년 전 조회 2,673
11년 전 조회 2,340
11년 전 조회 2,658
11년 전 조회 2,350
11년 전 조회 2,369
11년 전 조회 2,491
11년 전 조회 2,246
11년 전 조회 2,296
11년 전 조회 2,457
11년 전 조회 3,350
11년 전 조회 2,302
11년 전 조회 2,308
11년 전 조회 2,189
11년 전 조회 2,660
11년 전 조회 2,308
11년 전 조회 2,200
11년 전 조회 2,540
11년 전 조회 2,846
11년 전 조회 2,523
11년 전 조회 2,435
11년 전 조회 2,461
11년 전 조회 3,331
11년 전 조회 2,363
11년 전 조회 2,438
11년 전 조회 4,044
11년 전 조회 3,769
11년 전 조회 2,654
11년 전 조회 3,173
11년 전 조회 2,698
11년 전 조회 2,630