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

· 11년 전 · 3057
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,397
11년 전 조회 2,596
11년 전 조회 3,159
11년 전 조회 650
11년 전 조회 2,411
11년 전 조회 2,394
11년 전 조회 2,467
11년 전 조회 1,451
11년 전 조회 1,924
11년 전 조회 1,254
11년 전 조회 1,060
11년 전 조회 1,643
11년 전 조회 971
11년 전 조회 3,847
11년 전 조회 3,624
11년 전 조회 1,250
11년 전 조회 2,498
11년 전 조회 913
11년 전 조회 1,717
11년 전 조회 3,326
11년 전 조회 3,628
11년 전 조회 4,550
11년 전 조회 947
11년 전 조회 1,511
11년 전 조회 2,914
11년 전 조회 1,090
11년 전 조회 1,077
11년 전 조회 1,692
11년 전 조회 946
11년 전 조회 2,228
11년 전 조회 1,731
11년 전 조회 3,809
11년 전 조회 2,269
11년 전 조회 4,517
11년 전 조회 1,296
11년 전 조회 1,155
11년 전 조회 1,797
11년 전 조회 1,762
11년 전 조회 1,336
11년 전 조회 982
11년 전 조회 1,634
11년 전 조회 1,005
11년 전 조회 1,101
11년 전 조회 1,316
11년 전 조회 1,129
11년 전 조회 884
11년 전 조회 2,069
11년 전 조회 1,885
11년 전 조회 3,058
11년 전 조회 1,034
11년 전 조회 795
11년 전 조회 887
11년 전 조회 2,768
11년 전 조회 1,021
11년 전 조회 1,215
11년 전 조회 735
11년 전 조회 1,515
11년 전 조회 1,506
11년 전 조회 916
11년 전 조회 1,103
11년 전 조회 783
11년 전 조회 740
11년 전 조회 1,563
11년 전 조회 899
11년 전 조회 802
11년 전 조회 915
11년 전 조회 1,091
11년 전 조회 748
11년 전 조회 795
11년 전 조회 1,293
11년 전 조회 835
11년 전 조회 1,315
11년 전 조회 835
11년 전 조회 976
11년 전 조회 1,021
11년 전 조회 765
11년 전 조회 791
11년 전 조회 1,028
11년 전 조회 1,958
11년 전 조회 800
11년 전 조회 811
11년 전 조회 748
11년 전 조회 1,172
11년 전 조회 809
11년 전 조회 729
11년 전 조회 1,050
11년 전 조회 1,384
11년 전 조회 1,015
11년 전 조회 945
11년 전 조회 978
11년 전 조회 1,814
11년 전 조회 883
11년 전 조회 939
11년 전 조회 2,720
11년 전 조회 2,270
11년 전 조회 1,145
11년 전 조회 1,639
11년 전 조회 1,245
11년 전 조회 1,178
🐛 버그신고