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

· 11년 전 · 3095
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,057
11년 전 조회 3,437
11년 전 조회 2,643
11년 전 조회 3,192
11년 전 조회 691
11년 전 조회 2,463
11년 전 조회 2,437
11년 전 조회 2,509
11년 전 조회 1,495
11년 전 조회 1,973
11년 전 조회 1,297
11년 전 조회 1,103
11년 전 조회 1,684
11년 전 조회 1,007
11년 전 조회 3,889
11년 전 조회 3,666
11년 전 조회 1,299
11년 전 조회 2,537
11년 전 조회 948
11년 전 조회 1,760
11년 전 조회 3,364
11년 전 조회 3,667
11년 전 조회 4,588
11년 전 조회 986
11년 전 조회 1,547
11년 전 조회 2,950
11년 전 조회 1,126
11년 전 조회 1,121
11년 전 조회 1,733
11년 전 조회 992
11년 전 조회 2,265
11년 전 조회 1,767
11년 전 조회 3,855
11년 전 조회 2,307
11년 전 조회 4,552
11년 전 조회 1,331
11년 전 조회 1,193
11년 전 조회 1,832
11년 전 조회 1,799
11년 전 조회 1,373
11년 전 조회 1,018
11년 전 조회 1,663
11년 전 조회 1,041
11년 전 조회 1,140
11년 전 조회 1,349
11년 전 조회 1,170
11년 전 조회 921
11년 전 조회 2,104
11년 전 조회 1,925
11년 전 조회 3,096
11년 전 조회 1,075
11년 전 조회 829
11년 전 조회 929
11년 전 조회 2,801
11년 전 조회 1,054
11년 전 조회 1,249
11년 전 조회 770
11년 전 조회 1,549
11년 전 조회 1,536
11년 전 조회 944
11년 전 조회 1,131
11년 전 조회 813
11년 전 조회 776
11년 전 조회 1,595
11년 전 조회 932
11년 전 조회 828
11년 전 조회 943
11년 전 조회 1,111
11년 전 조회 778
11년 전 조회 824
11년 전 조회 1,310
11년 전 조회 868
11년 전 조회 1,334
11년 전 조회 857
11년 전 조회 998
11년 전 조회 1,051
11년 전 조회 794
11년 전 조회 821
11년 전 조회 1,052
11년 전 조회 1,984
11년 전 조회 833
11년 전 조회 843
11년 전 조회 777
11년 전 조회 1,202
11년 전 조회 842
11년 전 조회 760
11년 전 조회 1,073
11년 전 조회 1,414
11년 전 조회 1,046
11년 전 조회 968
11년 전 조회 1,011
11년 전 조회 1,845
11년 전 조회 915
11년 전 조회 974
11년 전 조회 2,748
11년 전 조회 2,296
11년 전 조회 1,171
11년 전 조회 1,664
11년 전 조회 1,273
11년 전 조회 1,208
🐛 버그신고