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

· 11년 전 · 3048
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,007
11년 전 조회 3,382
11년 전 조회 2,586
11년 전 조회 3,152
11년 전 조회 640
11년 전 조회 2,405
11년 전 조회 2,385
11년 전 조회 2,461
11년 전 조회 1,441
11년 전 조회 1,915
11년 전 조회 1,244
11년 전 조회 1,056
11년 전 조회 1,634
11년 전 조회 958
11년 전 조회 3,836
11년 전 조회 3,613
11년 전 조회 1,243
11년 전 조회 2,483
11년 전 조회 908
11년 전 조회 1,707
11년 전 조회 3,315
11년 전 조회 3,616
11년 전 조회 4,540
11년 전 조회 938
11년 전 조회 1,502
11년 전 조회 2,909
11년 전 조회 1,082
11년 전 조회 1,071
11년 전 조회 1,681
11년 전 조회 940
11년 전 조회 2,217
11년 전 조회 1,720
11년 전 조회 3,799
11년 전 조회 2,262
11년 전 조회 4,513
11년 전 조회 1,286
11년 전 조회 1,147
11년 전 조회 1,786
11년 전 조회 1,751
11년 전 조회 1,325
11년 전 조회 973
11년 전 조회 1,624
11년 전 조회 996
11년 전 조회 1,093
11년 전 조회 1,307
11년 전 조회 1,123
11년 전 조회 878
11년 전 조회 2,060
11년 전 조회 1,874
11년 전 조회 3,049
11년 전 조회 1,024
11년 전 조회 789
11년 전 조회 880
11년 전 조회 2,761
11년 전 조회 1,013
11년 전 조회 1,209
11년 전 조회 727
11년 전 조회 1,505
11년 전 조회 1,501
11년 전 조회 908
11년 전 조회 1,093
11년 전 조회 772
11년 전 조회 733
11년 전 조회 1,558
11년 전 조회 894
11년 전 조회 793
11년 전 조회 906
11년 전 조회 1,089
11년 전 조회 741
11년 전 조회 786
11년 전 조회 1,289
11년 전 조회 830
11년 전 조회 1,312
11년 전 조회 828
11년 전 조회 969
11년 전 조회 1,012
11년 전 조회 759
11년 전 조회 785
11년 전 조회 1,020
11년 전 조회 1,955
11년 전 조회 792
11년 전 조회 803
11년 전 조회 742
11년 전 조회 1,161
11년 전 조회 803
11년 전 조회 724
11년 전 조회 1,044
11년 전 조회 1,380
11년 전 조회 1,008
11년 전 조회 936
11년 전 조회 972
11년 전 조회 1,805
11년 전 조회 876
11년 전 조회 935
11년 전 조회 2,717
11년 전 조회 2,265
11년 전 조회 1,142
11년 전 조회 1,635
11년 전 조회 1,238
11년 전 조회 1,170
🐛 버그신고