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

· 11년 전 · 3143
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,101
11년 전 조회 3,473
11년 전 조회 2,682
11년 전 조회 3,235
11년 전 조회 737
11년 전 조회 2,503
11년 전 조회 2,475
11년 전 조회 2,555
11년 전 조회 1,534
11년 전 조회 2,011
11년 전 조회 1,339
11년 전 조회 1,140
11년 전 조회 1,723
11년 전 조회 1,047
11년 전 조회 3,934
11년 전 조회 3,704
11년 전 조회 1,339
11년 전 조회 2,573
11년 전 조회 995
11년 전 조회 1,801
11년 전 조회 3,411
11년 전 조회 3,713
11년 전 조회 4,634
11년 전 조회 1,030
11년 전 조회 1,587
11년 전 조회 2,992
11년 전 조회 1,174
11년 전 조회 1,166
11년 전 조회 1,775
11년 전 조회 1,039
11년 전 조회 2,307
11년 전 조회 1,816
11년 전 조회 3,897
11년 전 조회 2,350
11년 전 조회 4,598
11년 전 조회 1,376
11년 전 조회 1,239
11년 전 조회 1,883
11년 전 조회 1,850
11년 전 조회 1,422
11년 전 조회 1,070
11년 전 조회 1,701
11년 전 조회 1,090
11년 전 조회 1,188
11년 전 조회 1,399
11년 전 조회 1,219
11년 전 조회 968
11년 전 조회 2,148
11년 전 조회 1,971
11년 전 조회 3,144
11년 전 조회 1,108
11년 전 조회 881
11년 전 조회 977
11년 전 조회 2,848
11년 전 조회 1,089
11년 전 조회 1,291
11년 전 조회 820
11년 전 조회 1,594
11년 전 조회 1,587
11년 전 조회 995
11년 전 조회 1,175
11년 전 조회 860
11년 전 조회 812
11년 전 조회 1,641
11년 전 조회 973
11년 전 조회 875
11년 전 조회 990
11년 전 조회 1,165
11년 전 조회 827
11년 전 조회 865
11년 전 조회 1,351
11년 전 조회 914
11년 전 조회 1,389
11년 전 조회 902
11년 전 조회 1,036
11년 전 조회 1,087
11년 전 조회 839
11년 전 조회 869
11년 전 조회 1,091
11년 전 조회 2,028
11년 전 조회 869
11년 전 조회 892
11년 전 조회 822
11년 전 조회 1,241
11년 전 조회 895
11년 전 조회 804
11년 전 조회 1,114
11년 전 조회 1,456
11년 전 조회 1,092
11년 전 조회 1,020
11년 전 조회 1,064
11년 전 조회 1,895
11년 전 조회 957
11년 전 조회 1,017
11년 전 조회 2,796
11년 전 조회 2,351
11년 전 조회 1,221
11년 전 조회 1,716
11년 전 조회 1,319
11년 전 조회 1,250
🐛 버그신고