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

· 11년 전 · 3257
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,225
11년 전 조회 3,587
11년 전 조회 2,807
11년 전 조회 3,336
11년 전 조회 862
11년 전 조회 2,618
11년 전 조회 2,584
11년 전 조회 2,660
11년 전 조회 1,655
11년 전 조회 2,124
11년 전 조회 1,459
11년 전 조회 1,241
11년 전 조회 1,859
11년 전 조회 1,174
11년 전 조회 4,041
11년 전 조회 3,830
11년 전 조회 1,449
11년 전 조회 2,695
11년 전 조회 1,122
11년 전 조회 1,910
11년 전 조회 3,519
11년 전 조회 3,820
11년 전 조회 4,744
11년 전 조회 1,151
11년 전 조회 1,694
11년 전 조회 3,097
11년 전 조회 1,295
11년 전 조회 1,293
11년 전 조회 1,879
11년 전 조회 1,175
11년 전 조회 2,427
11년 전 조회 1,923
11년 전 조회 3,994
11년 전 조회 2,453
11년 전 조회 4,714
11년 전 조회 1,513
11년 전 조회 1,359
11년 전 조회 2,002
11년 전 조회 1,970
11년 전 조회 1,549
11년 전 조회 1,182
11년 전 조회 1,829
11년 전 조회 1,214
11년 전 조회 1,305
11년 전 조회 1,524
11년 전 조회 1,338
11년 전 조회 1,076
11년 전 조회 2,280
11년 전 조회 2,085
11년 전 조회 3,258
11년 전 조회 1,233
11년 전 조회 996
11년 전 조회 1,092
11년 전 조회 2,965
11년 전 조회 1,205
11년 전 조회 1,406
11년 전 조회 951
11년 전 조회 1,716
11년 전 조회 1,715
11년 전 조회 1,114
11년 전 조회 1,320
11년 전 조회 977
11년 전 조회 942
11년 전 조회 1,762
11년 전 조회 1,098
11년 전 조회 995
11년 전 조회 1,125
11년 전 조회 1,250
11년 전 조회 961
11년 전 조회 997
11년 전 조회 1,447
11년 전 조회 1,053
11년 전 조회 1,463
11년 전 조회 1,055
11년 전 조회 1,140
11년 전 조회 1,220
11년 전 조회 960
11년 전 조회 990
11년 전 조회 1,215
11년 전 조회 2,124
11년 전 조회 999
11년 전 조회 1,006
11년 전 조회 939
11년 전 조회 1,393
11년 전 조회 1,014
11년 전 조회 950
11년 전 조회 1,238
11년 전 조회 1,597
11년 전 조회 1,217
11년 전 조회 1,168
11년 전 조회 1,185
11년 전 조회 2,023
11년 전 조회 1,088
11년 전 조회 1,143
11년 전 조회 2,945
11년 전 조회 2,499
11년 전 조회 1,360
11년 전 조회 1,857
11년 전 조회 1,449
11년 전 조회 1,380