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

· 11년 전 · 3055
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,014
11년 전 조회 3,394
11년 전 조회 2,594
11년 전 조회 3,159
11년 전 조회 649
11년 전 조회 2,409
11년 전 조회 2,392
11년 전 조회 2,466
11년 전 조회 1,449
11년 전 조회 1,922
11년 전 조회 1,252
11년 전 조회 1,060
11년 전 조회 1,642
11년 전 조회 971
11년 전 조회 3,845
11년 전 조회 3,622
11년 전 조회 1,249
11년 전 조회 2,495
11년 전 조회 912
11년 전 조회 1,717
11년 전 조회 3,324
11년 전 조회 3,625
11년 전 조회 4,548
11년 전 조회 947
11년 전 조회 1,509
11년 전 조회 2,913
11년 전 조회 1,089
11년 전 조회 1,076
11년 전 조회 1,690
11년 전 조회 945
11년 전 조회 2,227
11년 전 조회 1,730
11년 전 조회 3,808
11년 전 조회 2,269
11년 전 조회 4,516
11년 전 조회 1,296
11년 전 조회 1,153
11년 전 조회 1,796
11년 전 조회 1,760
11년 전 조회 1,334
11년 전 조회 981
11년 전 조회 1,633
11년 전 조회 1,005
11년 전 조회 1,099
11년 전 조회 1,316
11년 전 조회 1,128
11년 전 조회 884
11년 전 조회 2,068
11년 전 조회 1,884
11년 전 조회 3,056
11년 전 조회 1,032
11년 전 조회 795
11년 전 조회 887
11년 전 조회 2,767
11년 전 조회 1,021
11년 전 조회 1,215
11년 전 조회 734
11년 전 조회 1,515
11년 전 조회 1,506
11년 전 조회 916
11년 전 조회 1,101
11년 전 조회 781
11년 전 조회 739
11년 전 조회 1,563
11년 전 조회 898
11년 전 조회 801
11년 전 조회 914
11년 전 조회 1,091
11년 전 조회 747
11년 전 조회 794
11년 전 조회 1,293
11년 전 조회 835
11년 전 조회 1,315
11년 전 조회 835
11년 전 조회 975
11년 전 조회 1,020
11년 전 조회 764
11년 전 조회 791
11년 전 조회 1,027
11년 전 조회 1,958
11년 전 조회 799
11년 전 조회 811
11년 전 조회 746
11년 전 조회 1,172
11년 전 조회 808
11년 전 조회 729
11년 전 조회 1,049
11년 전 조회 1,384
11년 전 조회 1,015
11년 전 조회 944
11년 전 조회 978
11년 전 조회 1,813
11년 전 조회 882
11년 전 조회 938
11년 전 조회 2,719
11년 전 조회 2,269
11년 전 조회 1,144
11년 전 조회 1,639
11년 전 조회 1,244
11년 전 조회 1,176
🐛 버그신고