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

· 11년 전 · 3058
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,016
11년 전 조회 3,398
11년 전 조회 2,596
11년 전 조회 3,159
11년 전 조회 650
11년 전 조회 2,411
11년 전 조회 2,395
11년 전 조회 2,469
11년 전 조회 1,451
11년 전 조회 1,925
11년 전 조회 1,254
11년 전 조회 1,063
11년 전 조회 1,646
11년 전 조회 973
11년 전 조회 3,847
11년 전 조회 3,625
11년 전 조회 1,251
11년 전 조회 2,499
11년 전 조회 914
11년 전 조회 1,719
11년 전 조회 3,327
11년 전 조회 3,628
11년 전 조회 4,550
11년 전 조회 947
11년 전 조회 1,512
11년 전 조회 2,914
11년 전 조회 1,091
11년 전 조회 1,078
11년 전 조회 1,692
11년 전 조회 947
11년 전 조회 2,228
11년 전 조회 1,734
11년 전 조회 3,810
11년 전 조회 2,270
11년 전 조회 4,517
11년 전 조회 1,298
11년 전 조회 1,155
11년 전 조회 1,798
11년 전 조회 1,763
11년 전 조회 1,337
11년 전 조회 983
11년 전 조회 1,636
11년 전 조회 1,006
11년 전 조회 1,101
11년 전 조회 1,318
11년 전 조회 1,130
11년 전 조회 885
11년 전 조회 2,071
11년 전 조회 1,885
11년 전 조회 3,059
11년 전 조회 1,034
11년 전 조회 797
11년 전 조회 887
11년 전 조회 2,769
11년 전 조회 1,021
11년 전 조회 1,218
11년 전 조회 735
11년 전 조회 1,515
11년 전 조회 1,508
11년 전 조회 917
11년 전 조회 1,106
11년 전 조회 783
11년 전 조회 743
11년 전 조회 1,564
11년 전 조회 899
11년 전 조회 802
11년 전 조회 915
11년 전 조회 1,091
11년 전 조회 748
11년 전 조회 795
11년 전 조회 1,293
11년 전 조회 837
11년 전 조회 1,315
11년 전 조회 835
11년 전 조회 976
11년 전 조회 1,021
11년 전 조회 766
11년 전 조회 791
11년 전 조회 1,029
11년 전 조회 1,959
11년 전 조회 800
11년 전 조회 814
11년 전 조회 749
11년 전 조회 1,173
11년 전 조회 809
11년 전 조회 730
11년 전 조회 1,052
11년 전 조회 1,384
11년 전 조회 1,017
11년 전 조회 945
11년 전 조회 979
11년 전 조회 1,814
11년 전 조회 884
11년 전 조회 940
11년 전 조회 2,721
11년 전 조회 2,270
11년 전 조회 1,147
11년 전 조회 1,640
11년 전 조회 1,247
11년 전 조회 1,179
🐛 버그신고