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

· 11년 전 · 3035
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년 전 조회 989
11년 전 조회 3,374
11년 전 조회 2,571
11년 전 조회 3,146
11년 전 조회 622
11년 전 조회 2,387
11년 전 조회 2,373
11년 전 조회 2,449
11년 전 조회 1,430
11년 전 조회 1,904
11년 전 조회 1,228
11년 전 조회 1,045
11년 전 조회 1,618
11년 전 조회 945
11년 전 조회 3,825
11년 전 조회 3,598
11년 전 조회 1,223
11년 전 조회 2,471
11년 전 조회 888
11년 전 조회 1,697
11년 전 조회 3,302
11년 전 조회 3,607
11년 전 조회 4,528
11년 전 조회 925
11년 전 조회 1,495
11년 전 조회 2,902
11년 전 조회 1,067
11년 전 조회 1,053
11년 전 조회 1,666
11년 전 조회 922
11년 전 조회 2,205
11년 전 조회 1,709
11년 전 조회 3,787
11년 전 조회 2,251
11년 전 조회 4,505
11년 전 조회 1,271
11년 전 조회 1,130
11년 전 조회 1,773
11년 전 조회 1,738
11년 전 조회 1,310
11년 전 조회 957
11년 전 조회 1,606
11년 전 조회 980
11년 전 조회 1,077
11년 전 조회 1,290
11년 전 조회 1,109
11년 전 조회 870
11년 전 조회 2,045
11년 전 조회 1,864
11년 전 조회 3,036
11년 전 조회 1,011
11년 전 조회 776
11년 전 조회 863
11년 전 조회 2,747
11년 전 조회 1,005
11년 전 조회 1,196
11년 전 조회 709
11년 전 조회 1,493
11년 전 조회 1,483
11년 전 조회 895
11년 전 조회 1,075
11년 전 조회 758
11년 전 조회 717
11년 전 조회 1,545
11년 전 조회 876
11년 전 조회 779
11년 전 조회 896
11년 전 조회 1,082
11년 전 조회 727
11년 전 조회 773
11년 전 조회 1,282
11년 전 조회 812
11년 전 조회 1,306
11년 전 조회 809
11년 전 조회 965
11년 전 조회 999
11년 전 조회 744
11년 전 조회 774
11년 전 조회 1,013
11년 전 조회 1,951
11년 전 조회 781
11년 전 조회 793
11년 전 조회 733
11년 전 조회 1,143
11년 전 조회 792
11년 전 조회 707
11년 전 조회 1,035
11년 전 조회 1,364
11년 전 조회 998
11년 전 조회 916
11년 전 조회 962
11년 전 조회 1,790
11년 전 조회 862
11년 전 조회 921
11년 전 조회 2,699
11년 전 조회 2,250
11년 전 조회 1,130
11년 전 조회 1,618
11년 전 조회 1,231
11년 전 조회 1,158
🐛 버그신고