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

· 11년 전 · 3049
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,009
11년 전 조회 3,389
11년 전 조회 2,590
11년 전 조회 3,154
11년 전 조회 644
11년 전 조회 2,408
11년 전 조회 2,386
11년 전 조회 2,462
11년 전 조회 1,445
11년 전 조회 1,918
11년 전 조회 1,247
11년 전 조회 1,057
11년 전 조회 1,639
11년 전 조회 964
11년 전 조회 3,841
11년 전 조회 3,618
11년 전 조회 1,246
11년 전 조회 2,490
11년 전 조회 910
11년 전 조회 1,711
11년 전 조회 3,319
11년 전 조회 3,618
11년 전 조회 4,543
11년 전 조회 945
11년 전 조회 1,504
11년 전 조회 2,912
11년 전 조회 1,085
11년 전 조회 1,074
11년 전 조회 1,686
11년 전 조회 943
11년 전 조회 2,219
11년 전 조회 1,722
11년 전 조회 3,802
11년 전 조회 2,263
11년 전 조회 4,513
11년 전 조회 1,292
11년 전 조회 1,150
11년 전 조회 1,789
11년 전 조회 1,754
11년 전 조회 1,330
11년 전 조회 975
11년 전 조회 1,629
11년 전 조회 1,002
11년 전 조회 1,095
11년 전 조회 1,311
11년 전 조회 1,126
11년 전 조회 880
11년 전 조회 2,065
11년 전 조회 1,879
11년 전 조회 3,050
11년 전 조회 1,028
11년 전 조회 792
11년 전 조회 883
11년 전 조회 2,763
11년 전 조회 1,017
11년 전 조회 1,212
11년 전 조회 730
11년 전 조회 1,510
11년 전 조회 1,503
11년 전 조회 912
11년 전 조회 1,098
11년 전 조회 778
11년 전 조회 736
11년 전 조회 1,560
11년 전 조회 895
11년 전 조회 796
11년 전 조회 908
11년 전 조회 1,089
11년 전 조회 744
11년 전 조회 788
11년 전 조회 1,289
11년 전 조회 831
11년 전 조회 1,313
11년 전 조회 830
11년 전 조회 972
11년 전 조회 1,014
11년 전 조회 760
11년 전 조회 787
11년 전 조회 1,023
11년 전 조회 1,957
11년 전 조회 794
11년 전 조회 804
11년 전 조회 745
11년 전 조회 1,166
11년 전 조회 805
11년 전 조회 728
11년 전 조회 1,046
11년 전 조회 1,383
11년 전 조회 1,009
11년 전 조회 938
11년 전 조회 975
11년 전 조회 1,808
11년 전 조회 878
11년 전 조회 937
11년 전 조회 2,718
11년 전 조회 2,267
11년 전 조회 1,143
11년 전 조회 1,637
11년 전 조회 1,240
11년 전 조회 1,173
🐛 버그신고