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

· 11년 전 · 3037
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년 전 조회 990
11년 전 조회 3,375
11년 전 조회 2,571
11년 전 조회 3,146
11년 전 조회 624
11년 전 조회 2,387
11년 전 조회 2,375
11년 전 조회 2,451
11년 전 조회 1,430
11년 전 조회 1,904
11년 전 조회 1,228
11년 전 조회 1,046
11년 전 조회 1,619
11년 전 조회 945
11년 전 조회 3,825
11년 전 조회 3,598
11년 전 조회 1,223
11년 전 조회 2,471
11년 전 조회 889
11년 전 조회 1,698
11년 전 조회 3,304
11년 전 조회 3,607
11년 전 조회 4,528
11년 전 조회 925
11년 전 조회 1,496
11년 전 조회 2,905
11년 전 조회 1,068
11년 전 조회 1,053
11년 전 조회 1,667
11년 전 조회 922
11년 전 조회 2,205
11년 전 조회 1,713
11년 전 조회 3,788
11년 전 조회 2,254
11년 전 조회 4,506
11년 전 조회 1,272
11년 전 조회 1,130
11년 전 조회 1,775
11년 전 조회 1,739
11년 전 조회 1,310
11년 전 조회 958
11년 전 조회 1,608
11년 전 조회 980
11년 전 조회 1,078
11년 전 조회 1,291
11년 전 조회 1,109
11년 전 조회 873
11년 전 조회 2,045
11년 전 조회 1,865
11년 전 조회 3,038
11년 전 조회 1,011
11년 전 조회 780
11년 전 조회 863
11년 전 조회 2,747
11년 전 조회 1,006
11년 전 조회 1,196
11년 전 조회 709
11년 전 조회 1,494
11년 전 조회 1,484
11년 전 조회 896
11년 전 조회 1,076
11년 전 조회 758
11년 전 조회 717
11년 전 조회 1,546
11년 전 조회 876
11년 전 조회 781
11년 전 조회 896
11년 전 조회 1,082
11년 전 조회 727
11년 전 조회 773
11년 전 조회 1,282
11년 전 조회 813
11년 전 조회 1,306
11년 전 조회 809
11년 전 조회 965
11년 전 조회 999
11년 전 조회 744
11년 전 조회 774
11년 전 조회 1,013
11년 전 조회 1,951
11년 전 조회 782
11년 전 조회 794
11년 전 조회 733
11년 전 조회 1,143
11년 전 조회 792
11년 전 조회 707
11년 전 조회 1,035
11년 전 조회 1,364
11년 전 조회 999
11년 전 조회 916
11년 전 조회 962
11년 전 조회 1,791
11년 전 조회 862
11년 전 조회 921
11년 전 조회 2,699
11년 전 조회 2,251
11년 전 조회 1,131
11년 전 조회 1,618
11년 전 조회 1,231
11년 전 조회 1,158
🐛 버그신고