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

· 11년 전 · 4339
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,246
11년 전 조회 4,618
11년 전 조회 3,875
11년 전 조회 4,367
11년 전 조회 1,906
11년 전 조회 3,684
11년 전 조회 3,681
11년 전 조회 3,754
11년 전 조회 2,742
11년 전 조회 3,197
11년 전 조회 2,544
11년 전 조회 2,326
11년 전 조회 2,951
11년 전 조회 2,247
11년 전 조회 5,121
11년 전 조회 4,833
11년 전 조회 2,535
11년 전 조회 3,754
11년 전 조회 2,173
11년 전 조회 2,936
11년 전 조회 4,561
11년 전 조회 4,892
11년 전 조회 5,797
11년 전 조회 2,169
11년 전 조회 2,741
11년 전 조회 4,196
11년 전 조회 2,343
11년 전 조회 2,352
11년 전 조회 2,925
11년 전 조회 2,220
11년 전 조회 3,462
11년 전 조회 2,987
11년 전 조회 5,056
11년 전 조회 3,490
11년 전 조회 5,761
11년 전 조회 2,568
11년 전 조회 2,420
11년 전 조회 3,084
11년 전 조회 3,067
11년 전 조회 2,607
11년 전 조회 2,245
11년 전 조회 2,866
11년 전 조회 2,313
11년 전 조회 2,378
11년 전 조회 2,607
11년 전 조회 2,441
11년 전 조회 2,150
11년 전 조회 3,351
11년 전 조회 3,180
11년 전 조회 4,340
11년 전 조회 2,345
11년 전 조회 2,104
11년 전 조회 2,189
11년 전 조회 4,080
11년 전 조회 2,288
11년 전 조회 2,517
11년 전 조회 2,045
11년 전 조회 2,754
11년 전 조회 2,772
11년 전 조회 2,191
11년 전 조회 2,377
11년 전 조회 2,049
11년 전 조회 1,992
11년 전 조회 2,801
11년 전 조회 2,149
11년 전 조회 2,058
11년 전 조회 2,206
11년 전 조회 2,318
11년 전 조회 2,006
11년 전 조회 2,073
11년 전 조회 2,510
11년 전 조회 2,125
11년 전 조회 2,499
11년 전 조회 2,166
11년 전 조회 2,197
11년 전 조회 2,312
11년 전 조회 2,048
11년 전 조회 2,096
11년 전 조회 2,294
11년 전 조회 3,187
11년 전 조회 2,113
11년 전 조회 2,118
11년 전 조회 2,010
11년 전 조회 2,472
11년 전 조회 2,118
11년 전 조회 2,032
11년 전 조회 2,341
11년 전 조회 2,682
11년 전 조회 2,354
11년 전 조회 2,253
11년 전 조회 2,278
11년 전 조회 3,154
11년 전 조회 2,199
11년 전 조회 2,273
11년 전 조회 3,980
11년 전 조회 3,588
11년 전 조회 2,472
11년 전 조회 3,007
11년 전 조회 2,515
11년 전 조회 2,444