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

· 11년 전 · 3108
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,066
11년 전 조회 3,444
11년 전 조회 2,652
11년 전 조회 3,201
11년 전 조회 698
11년 전 조회 2,475
11년 전 조회 2,446
11년 전 조회 2,515
11년 전 조회 1,502
11년 전 조회 1,983
11년 전 조회 1,308
11년 전 조회 1,110
11년 전 조회 1,694
11년 전 조회 1,012
11년 전 조회 3,900
11년 전 조회 3,676
11년 전 조회 1,309
11년 전 조회 2,541
11년 전 조회 959
11년 전 조회 1,768
11년 전 조회 3,372
11년 전 조회 3,679
11년 전 조회 4,597
11년 전 조회 995
11년 전 조회 1,555
11년 전 조회 2,959
11년 전 조회 1,136
11년 전 조회 1,129
11년 전 조회 1,741
11년 전 조회 999
11년 전 조회 2,276
11년 전 조회 1,776
11년 전 조회 3,864
11년 전 조회 2,314
11년 전 조회 4,560
11년 전 조회 1,336
11년 전 조회 1,205
11년 전 조회 1,844
11년 전 조회 1,811
11년 전 조회 1,381
11년 전 조회 1,027
11년 전 조회 1,672
11년 전 조회 1,049
11년 전 조회 1,151
11년 전 조회 1,357
11년 전 조회 1,181
11년 전 조회 929
11년 전 조회 2,116
11년 전 조회 1,939
11년 전 조회 3,109
11년 전 조회 1,081
11년 전 조회 837
11년 전 조회 936
11년 전 조회 2,813
11년 전 조회 1,061
11년 전 조회 1,256
11년 전 조회 782
11년 전 조회 1,563
11년 전 조회 1,544
11년 전 조회 956
11년 전 조회 1,137
11년 전 조회 823
11년 전 조회 781
11년 전 조회 1,608
11년 전 조회 942
11년 전 조회 836
11년 전 조회 958
11년 전 조회 1,122
11년 전 조회 790
11년 전 조회 835
11년 전 조회 1,319
11년 전 조회 875
11년 전 조회 1,344
11년 전 조회 862
11년 전 조회 1,006
11년 전 조회 1,061
11년 전 조회 803
11년 전 조회 831
11년 전 조회 1,058
11년 전 조회 1,995
11년 전 조회 842
11년 전 조회 851
11년 전 조회 785
11년 전 조회 1,207
11년 전 조회 853
11년 전 조회 771
11년 전 조회 1,077
11년 전 조회 1,423
11년 전 조회 1,057
11년 전 조회 977
11년 전 조회 1,020
11년 전 조회 1,859
11년 전 조회 924
11년 전 조회 983
11년 전 조회 2,759
11년 전 조회 2,305
11년 전 조회 1,179
11년 전 조회 1,670
11년 전 조회 1,285
11년 전 조회 1,218
🐛 버그신고