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,088
11년 전 조회 3,461
11년 전 조회 2,667
11년 전 조회 3,218
11년 전 조회 721
11년 전 조회 2,497
11년 전 조회 2,462
11년 전 조회 2,541
11년 전 조회 1,518
11년 전 조회 2,003
11년 전 조회 1,329
11년 전 조회 1,125
11년 전 조회 1,712
11년 전 조회 1,035
11년 전 조회 3,920
11년 전 조회 3,694
11년 전 조회 1,328
11년 전 조회 2,562
11년 전 조회 985
11년 전 조회 1,788
11년 전 조회 3,395
11년 전 조회 3,695
11년 전 조회 4,619
11년 전 조회 1,014
11년 전 조회 1,575
11년 전 조회 2,982
11년 전 조회 1,163
11년 전 조회 1,154
11년 전 조회 1,761
11년 전 조회 1,024
11년 전 조회 2,296
11년 전 조회 1,799
11년 전 조회 3,885
11년 전 조회 2,336
11년 전 조회 4,586
11년 전 조회 1,356
11년 전 조회 1,226
11년 전 조회 1,865
11년 전 조회 1,832
11년 전 조회 1,402
11년 전 조회 1,051
11년 전 조회 1,688
11년 전 조회 1,074
11년 전 조회 1,173
11년 전 조회 1,388
11년 전 조회 1,205
11년 전 조회 951
11년 전 조회 2,141
11년 전 조회 1,962
11년 전 조회 3,128
11년 전 조회 1,101
11년 전 조회 863
11년 전 조회 959
11년 전 조회 2,831
11년 전 조회 1,079
11년 전 조회 1,280
11년 전 조회 802
11년 전 조회 1,582
11년 전 조회 1,573
11년 전 조회 982
11년 전 조회 1,161
11년 전 조회 844
11년 전 조회 801
11년 전 조회 1,628
11년 전 조회 960
11년 전 조회 861
11년 전 조회 974
11년 전 조회 1,140
11년 전 조회 809
11년 전 조회 853
11년 전 조회 1,337
11년 전 조회 899
11년 전 조회 1,365
11년 전 조회 885
11년 전 조회 1,020
11년 전 조회 1,076
11년 전 조회 823
11년 전 조회 853
11년 전 조회 1,076
11년 전 조회 2,010
11년 전 조회 857
11년 전 조회 874
11년 전 조회 804
11년 전 조회 1,230
11년 전 조회 878
11년 전 조회 787
11년 전 조회 1,100
11년 전 조회 1,440
11년 전 조회 1,075
11년 전 조회 1,000
11년 전 조회 1,044
11년 전 조회 1,877
11년 전 조회 942
11년 전 조회 998
11년 전 조회 2,782
11년 전 조회 2,328
11년 전 조회 1,204
11년 전 조회 1,698
11년 전 조회 1,304
11년 전 조회 1,232
🐛 버그신고