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

· 11년 전 · 3127
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,087
11년 전 조회 3,460
11년 전 조회 2,666
11년 전 조회 3,218
11년 전 조회 719
11년 전 조회 2,496
11년 전 조회 2,461
11년 전 조회 2,538
11년 전 조회 1,518
11년 전 조회 2,003
11년 전 조회 1,329
11년 전 조회 1,124
11년 전 조회 1,712
11년 전 조회 1,033
11년 전 조회 3,920
11년 전 조회 3,694
11년 전 조회 1,327
11년 전 조회 2,561
11년 전 조회 984
11년 전 조회 1,787
11년 전 조회 3,394
11년 전 조회 3,695
11년 전 조회 4,619
11년 전 조회 1,013
11년 전 조회 1,575
11년 전 조회 2,981
11년 전 조회 1,162
11년 전 조회 1,153
11년 전 조회 1,761
11년 전 조회 1,023
11년 전 조회 2,296
11년 전 조회 1,798
11년 전 조회 3,883
11년 전 조회 2,334
11년 전 조회 4,585
11년 전 조회 1,355
11년 전 조회 1,226
11년 전 조회 1,865
11년 전 조회 1,831
11년 전 조회 1,401
11년 전 조회 1,051
11년 전 조회 1,687
11년 전 조회 1,073
11년 전 조회 1,173
11년 전 조회 1,386
11년 전 조회 1,205
11년 전 조회 949
11년 전 조회 2,139
11년 전 조회 1,961
11년 전 조회 3,127
11년 전 조회 1,101
11년 전 조회 862
11년 전 조회 958
11년 전 조회 2,831
11년 전 조회 1,078
11년 전 조회 1,279
11년 전 조회 801
11년 전 조회 1,581
11년 전 조회 1,569
11년 전 조회 982
11년 전 조회 1,160
11년 전 조회 844
11년 전 조회 800
11년 전 조회 1,628
11년 전 조회 959
11년 전 조회 859
11년 전 조회 974
11년 전 조회 1,138
11년 전 조회 809
11년 전 조회 853
11년 전 조회 1,335
11년 전 조회 897
11년 전 조회 1,363
11년 전 조회 884
11년 전 조회 1,019
11년 전 조회 1,076
11년 전 조회 823
11년 전 조회 850
11년 전 조회 1,075
11년 전 조회 2,009
11년 전 조회 857
11년 전 조회 873
11년 전 조회 802
11년 전 조회 1,229
11년 전 조회 876
11년 전 조회 785
11년 전 조회 1,098
11년 전 조회 1,439
11년 전 조회 1,074
11년 전 조회 998
11년 전 조회 1,043
11년 전 조회 1,877
11년 전 조회 941
11년 전 조회 996
11년 전 조회 2,780
11년 전 조회 2,326
11년 전 조회 1,203
11년 전 조회 1,698
11년 전 조회 1,304
11년 전 조회 1,231
🐛 버그신고