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

· 11년 전 · 3362
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년 전 조회 1,321
11년 전 조회 3,695
11년 전 조회 2,907
11년 전 조회 3,428
11년 전 조회 954
11년 전 조회 2,717
11년 전 조회 2,690
11년 전 조회 2,767
11년 전 조회 1,761
11년 전 조회 2,223
11년 전 조회 1,553
11년 전 조회 1,338
11년 전 조회 1,962
11년 전 조회 1,269
11년 전 조회 4,143
11년 전 조회 3,927
11년 전 조회 1,560
11년 전 조회 2,803
11년 전 조회 1,216
11년 전 조회 2,020
11년 전 조회 3,618
11년 전 조회 3,931
11년 전 조회 4,848
11년 전 조회 1,243
11년 전 조회 1,790
11년 전 조회 3,184
11년 전 조회 1,396
11년 전 조회 1,385
11년 전 조회 1,983
11년 전 조회 1,270
11년 전 조회 2,520
11년 전 조회 2,026
11년 전 조회 4,103
11년 전 조회 2,544
11년 전 조회 4,809
11년 전 조회 1,604
11년 전 조회 1,458
11년 전 조회 2,098
11년 전 조회 2,070
11년 전 조회 1,650
11년 전 조회 1,284
11년 전 조회 1,930
11년 전 조회 1,316
11년 전 조회 1,411
11년 전 조회 1,621
11년 전 조회 1,425
11년 전 조회 1,171
11년 전 조회 2,376
11년 전 조회 2,195
11년 전 조회 3,363
11년 전 조회 1,345
11년 전 조회 1,099
11년 전 조회 1,186
11년 전 조회 3,061
11년 전 조회 1,298
11년 전 조회 1,505
11년 전 조회 1,049
11년 전 조회 1,807
11년 전 조회 1,791
11년 전 조회 1,209
11년 전 조회 1,410
11년 전 조회 1,077
11년 전 조회 1,037
11년 전 조회 1,852
11년 전 조회 1,180
11년 전 조회 1,088
11년 전 조회 1,225
11년 전 조회 1,345
11년 전 조회 1,052
11년 전 조회 1,089
11년 전 조회 1,516
11년 전 조회 1,147
11년 전 조회 1,555
11년 전 조회 1,146
11년 전 조회 1,233
11년 전 조회 1,305
11년 전 조회 1,063
11년 전 조회 1,091
11년 전 조회 1,311
11년 전 조회 2,222
11년 전 조회 1,105
11년 전 조회 1,103
11년 전 조회 1,041
11년 전 조회 1,485
11년 전 조회 1,101
11년 전 조회 1,045
11년 전 조회 1,330
11년 전 조회 1,693
11년 전 조회 1,305
11년 전 조회 1,279
11년 전 조회 1,282
11년 전 조회 2,112
11년 전 조회 1,180
11년 전 조회 1,249
11년 전 조회 3,038
11년 전 조회 2,592
11년 전 조회 1,460
11년 전 조회 1,961
11년 전 조회 1,543
11년 전 조회 1,470