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

· 11년 전 · 3361
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년 전 조회 953
11년 전 조회 2,717
11년 전 조회 2,690
11년 전 조회 2,764
11년 전 조회 1,761
11년 전 조회 2,222
11년 전 조회 1,552
11년 전 조회 1,335
11년 전 조회 1,959
11년 전 조회 1,268
11년 전 조회 4,143
11년 전 조회 3,926
11년 전 조회 1,560
11년 전 조회 2,803
11년 전 조회 1,215
11년 전 조회 2,020
11년 전 조회 3,618
11년 전 조회 3,931
11년 전 조회 4,848
11년 전 조회 1,243
11년 전 조회 1,788
11년 전 조회 3,183
11년 전 조회 1,395
11년 전 조회 1,384
11년 전 조회 1,983
11년 전 조회 1,268
11년 전 조회 2,520
11년 전 조회 2,026
11년 전 조회 4,103
11년 전 조회 2,544
11년 전 조회 4,809
11년 전 조회 1,603
11년 전 조회 1,458
11년 전 조회 2,098
11년 전 조회 2,070
11년 전 조회 1,650
11년 전 조회 1,284
11년 전 조회 1,929
11년 전 조회 1,316
11년 전 조회 1,409
11년 전 조회 1,620
11년 전 조회 1,425
11년 전 조회 1,169
11년 전 조회 2,375
11년 전 조회 2,194
11년 전 조회 3,362
11년 전 조회 1,345
11년 전 조회 1,098
11년 전 조회 1,186
11년 전 조회 3,061
11년 전 조회 1,297
11년 전 조회 1,505
11년 전 조회 1,049
11년 전 조회 1,806
11년 전 조회 1,789
11년 전 조회 1,208
11년 전 조회 1,409
11년 전 조회 1,077
11년 전 조회 1,037
11년 전 조회 1,851
11년 전 조회 1,180
11년 전 조회 1,088
11년 전 조회 1,225
11년 전 조회 1,341
11년 전 조회 1,052
11년 전 조회 1,089
11년 전 조회 1,515
11년 전 조회 1,147
11년 전 조회 1,555
11년 전 조회 1,146
11년 전 조회 1,232
11년 전 조회 1,304
11년 전 조회 1,063
11년 전 조회 1,091
11년 전 조회 1,310
11년 전 조회 2,222
11년 전 조회 1,105
11년 전 조회 1,103
11년 전 조회 1,041
11년 전 조회 1,483
11년 전 조회 1,101
11년 전 조회 1,045
11년 전 조회 1,330
11년 전 조회 1,691
11년 전 조회 1,305
11년 전 조회 1,278
11년 전 조회 1,282
11년 전 조회 2,112
11년 전 조회 1,179
11년 전 조회 1,248
11년 전 조회 3,038
11년 전 조회 2,591
11년 전 조회 1,458
11년 전 조회 1,960
11년 전 조회 1,541
11년 전 조회 1,470