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

· 11년 전 · 3046
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,000
11년 전 조회 3,380
11년 전 조회 2,580
11년 전 조회 3,150
11년 전 조회 634
11년 전 조회 2,400
11년 전 조회 2,382
11년 전 조회 2,458
11년 전 조회 1,438
11년 전 조회 1,912
11년 전 조회 1,238
11년 전 조회 1,052
11년 전 조회 1,631
11년 전 조회 957
11년 전 조회 3,836
11년 전 조회 3,610
11년 전 조회 1,234
11년 전 조회 2,480
11년 전 조회 904
11년 전 조회 1,704
11년 전 조회 3,310
11년 전 조회 3,614
11년 전 조회 4,538
11년 전 조회 936
11년 전 조회 1,502
11년 전 조회 2,908
11년 전 조회 1,076
11년 전 조회 1,067
11년 전 조회 1,678
11년 전 조회 936
11년 전 조회 2,215
11년 전 조회 1,718
11년 전 조회 3,798
11년 전 조회 2,260
11년 전 조회 4,513
11년 전 조회 1,283
11년 전 조회 1,144
11년 전 조회 1,783
11년 전 조회 1,750
11년 전 조회 1,322
11년 전 조회 970
11년 전 조회 1,623
11년 전 조회 993
11년 전 조회 1,090
11년 전 조회 1,305
11년 전 조회 1,119
11년 전 조회 877
11년 전 조회 2,058
11년 전 조회 1,873
11년 전 조회 3,047
11년 전 조회 1,021
11년 전 조회 782
11년 전 조회 875
11년 전 조회 2,759
11년 전 조회 1,011
11년 전 조회 1,206
11년 전 조회 723
11년 전 조회 1,504
11년 전 조회 1,497
11년 전 조회 906
11년 전 조회 1,089
11년 전 조회 769
11년 전 조회 731
11년 전 조회 1,555
11년 전 조회 890
11년 전 조회 789
11년 전 조회 905
11년 전 조회 1,087
11년 전 조회 739
11년 전 조회 782
11년 전 조회 1,288
11년 전 조회 825
11년 전 조회 1,309
11년 전 조회 825
11년 전 조회 969
11년 전 조회 1,009
11년 전 조회 756
11년 전 조회 784
11년 전 조회 1,020
11년 전 조회 1,955
11년 전 조회 790
11년 전 조회 802
11년 전 조회 742
11년 전 조회 1,158
11년 전 조회 802
11년 전 조회 723
11년 전 조회 1,044
11년 전 조회 1,379
11년 전 조회 1,008
11년 전 조회 931
11년 전 조회 972
11년 전 조회 1,801
11년 전 조회 874
11년 전 조회 932
11년 전 조회 2,712
11년 전 조회 2,262
11년 전 조회 1,139
11년 전 조회 1,631
11년 전 조회 1,238
11년 전 조회 1,167
🐛 버그신고