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

· 11년 전 · 3092
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,052
11년 전 조회 3,432
11년 전 조회 2,639
11년 전 조회 3,187
11년 전 조회 685
11년 전 조회 2,459
11년 전 조회 2,432
11년 전 조회 2,501
11년 전 조회 1,491
11년 전 조회 1,969
11년 전 조회 1,290
11년 전 조회 1,097
11년 전 조회 1,681
11년 전 조회 1,001
11년 전 조회 3,886
11년 전 조회 3,661
11년 전 조회 1,295
11년 전 조회 2,532
11년 전 조회 944
11년 전 조회 1,755
11년 전 조회 3,360
11년 전 조회 3,664
11년 전 조회 4,586
11년 전 조회 981
11년 전 조회 1,541
11년 전 조회 2,946
11년 전 조회 1,122
11년 전 조회 1,113
11년 전 조회 1,730
11년 전 조회 986
11년 전 조회 2,261
11년 전 조회 1,763
11년 전 조회 3,849
11년 전 조회 2,302
11년 전 조회 4,547
11년 전 조회 1,324
11년 전 조회 1,188
11년 전 조회 1,830
11년 전 조회 1,796
11년 전 조회 1,369
11년 전 조회 1,015
11년 전 조회 1,657
11년 전 조회 1,037
11년 전 조회 1,137
11년 전 조회 1,347
11년 전 조회 1,166
11년 전 조회 915
11년 전 조회 2,102
11년 전 조회 1,919
11년 전 조회 3,093
11년 전 조회 1,069
11년 전 조회 823
11년 전 조회 922
11년 전 조회 2,798
11년 전 조회 1,049
11년 전 조회 1,244
11년 전 조회 765
11년 전 조회 1,545
11년 전 조회 1,531
11년 전 조회 940
11년 전 조회 1,127
11년 전 조회 810
11년 전 조회 770
11년 전 조회 1,593
11년 전 조회 929
11년 전 조회 824
11년 전 조회 938
11년 전 조회 1,109
11년 전 조회 774
11년 전 조회 819
11년 전 조회 1,308
11년 전 조회 863
11년 전 조회 1,334
11년 전 조회 855
11년 전 조회 994
11년 전 조회 1,046
11년 전 조회 790
11년 전 조회 817
11년 전 조회 1,048
11년 전 조회 1,979
11년 전 조회 827
11년 전 조회 837
11년 전 조회 773
11년 전 조회 1,199
11년 전 조회 838
11년 전 조회 755
11년 전 조회 1,070
11년 전 조회 1,409
11년 전 조회 1,040
11년 전 조회 966
11년 전 조회 1,008
11년 전 조회 1,842
11년 전 조회 911
11년 전 조회 968
11년 전 조회 2,743
11년 전 조회 2,291
11년 전 조회 1,165
11년 전 조회 1,662
11년 전 조회 1,268
11년 전 조회 1,204
🐛 버그신고