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

· 11년 전 · 3123
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,083
11년 전 조회 3,457
11년 전 조회 2,662
11년 전 조회 3,214
11년 전 조회 711
11년 전 조회 2,487
11년 전 조회 2,457
11년 전 조회 2,531
11년 전 조회 1,511
11년 전 조회 1,999
11년 전 조회 1,326
11년 전 조회 1,123
11년 전 조회 1,708
11년 전 조회 1,026
11년 전 조회 3,912
11년 전 조회 3,690
11년 전 조회 1,320
11년 전 조회 2,558
11년 전 조회 975
11년 전 조회 1,781
11년 전 조회 3,389
11년 전 조회 3,690
11년 전 조회 4,614
11년 전 조회 1,007
11년 전 조회 1,569
11년 전 조회 2,974
11년 전 조회 1,154
11년 전 조회 1,147
11년 전 조회 1,755
11년 전 조회 1,016
11년 전 조회 2,288
11년 전 조회 1,794
11년 전 조회 3,878
11년 전 조회 2,330
11년 전 조회 4,579
11년 전 조회 1,349
11년 전 조회 1,218
11년 전 조회 1,860
11년 전 조회 1,826
11년 전 조회 1,392
11년 전 조회 1,042
11년 전 조회 1,681
11년 전 조회 1,065
11년 전 조회 1,168
11년 전 조회 1,378
11년 전 조회 1,200
11년 전 조회 945
11년 전 조회 2,133
11년 전 조회 1,955
11년 전 조회 3,123
11년 전 조회 1,093
11년 전 조회 855
11년 전 조회 953
11년 전 조회 2,826
11년 전 조회 1,075
11년 전 조회 1,273
11년 전 조회 795
11년 전 조회 1,575
11년 전 조회 1,561
11년 전 조회 974
11년 전 조회 1,151
11년 전 조회 838
11년 전 조회 793
11년 전 조회 1,623
11년 전 조회 955
11년 전 조회 853
11년 전 조회 969
11년 전 조회 1,131
11년 전 조회 803
11년 전 조회 847
11년 전 조회 1,331
11년 전 조회 888
11년 전 조회 1,357
11년 전 조회 879
11년 전 조회 1,013
11년 전 조회 1,071
11년 전 조회 818
11년 전 조회 841
11년 전 조회 1,071
11년 전 조회 2,003
11년 전 조회 853
11년 전 조회 866
11년 전 조회 797
11년 전 조회 1,221
11년 전 조회 865
11년 전 조회 780
11년 전 조회 1,088
11년 전 조회 1,433
11년 전 조회 1,066
11년 전 조회 991
11년 전 조회 1,035
11년 전 조회 1,871
11년 전 조회 935
11년 전 조회 993
11년 전 조회 2,774
11년 전 조회 2,322
11년 전 조회 1,194
11년 전 조회 1,687
11년 전 조회 1,297
11년 전 조회 1,228
🐛 버그신고