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

· 11년 전 · 3038
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년 전 조회 992
11년 전 조회 3,375
11년 전 조회 2,571
11년 전 조회 3,147
11년 전 조회 625
11년 전 조회 2,389
11년 전 조회 2,375
11년 전 조회 2,451
11년 전 조회 1,431
11년 전 조회 1,905
11년 전 조회 1,229
11년 전 조회 1,047
11년 전 조회 1,620
11년 전 조회 946
11년 전 조회 3,827
11년 전 조회 3,599
11년 전 조회 1,225
11년 전 조회 2,472
11년 전 조회 891
11년 전 조회 1,699
11년 전 조회 3,306
11년 전 조회 3,608
11년 전 조회 4,529
11년 전 조회 926
11년 전 조회 1,496
11년 전 조회 2,905
11년 전 조회 1,070
11년 전 조회 1,054
11년 전 조회 1,669
11년 전 조회 923
11년 전 조회 2,206
11년 전 조회 1,714
11년 전 조회 3,789
11년 전 조회 2,255
11년 전 조회 4,506
11년 전 조회 1,273
11년 전 조회 1,131
11년 전 조회 1,778
11년 전 조회 1,742
11년 전 조회 1,311
11년 전 조회 961
11년 전 조회 1,610
11년 전 조회 983
11년 전 조회 1,080
11년 전 조회 1,293
11년 전 조회 1,110
11년 전 조회 873
11년 전 조회 2,047
11년 전 조회 1,865
11년 전 조회 3,039
11년 전 조회 1,013
11년 전 조회 780
11년 전 조회 866
11년 전 조회 2,748
11년 전 조회 1,006
11년 전 조회 1,196
11년 전 조회 711
11년 전 조회 1,495
11년 전 조회 1,486
11년 전 조회 896
11년 전 조회 1,076
11년 전 조회 758
11년 전 조회 718
11년 전 조회 1,546
11년 전 조회 876
11년 전 조회 781
11년 전 조회 896
11년 전 조회 1,083
11년 전 조회 728
11년 전 조회 774
11년 전 조회 1,283
11년 전 조회 813
11년 전 조회 1,306
11년 전 조회 809
11년 전 조회 965
11년 전 조회 1,000
11년 전 조회 745
11년 전 조회 774
11년 전 조회 1,013
11년 전 조회 1,951
11년 전 조회 782
11년 전 조회 794
11년 전 조회 734
11년 전 조회 1,145
11년 전 조회 792
11년 전 조회 707
11년 전 조회 1,036
11년 전 조회 1,364
11년 전 조회 999
11년 전 조회 917
11년 전 조회 962
11년 전 조회 1,791
11년 전 조회 862
11년 전 조회 921
11년 전 조회 2,700
11년 전 조회 2,251
11년 전 조회 1,131
11년 전 조회 1,618
11년 전 조회 1,231
11년 전 조회 1,158
🐛 버그신고