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

· 11년 전 · 3211
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,164
11년 전 조회 3,540
11년 전 조회 2,745
11년 전 조회 3,290
11년 전 조회 799
11년 전 조회 2,564
11년 전 조회 2,532
11년 전 조회 2,614
11년 전 조회 1,604
11년 전 조회 2,071
11년 전 조회 1,406
11년 전 조회 1,204
11년 전 조회 1,794
11년 전 조회 1,108
11년 전 조회 4,000
11년 전 조회 3,779
11년 전 조회 1,398
11년 전 조회 2,648
11년 전 조회 1,061
11년 전 조회 1,869
11년 전 조회 3,473
11년 전 조회 3,782
11년 전 조회 4,701
11년 전 조회 1,091
11년 전 조회 1,653
11년 전 조회 3,054
11년 전 조회 1,235
11년 전 조회 1,237
11년 전 조회 1,840
11년 전 조회 1,114
11년 전 조회 2,378
11년 전 조회 1,880
11년 전 조회 3,963
11년 전 조회 2,405
11년 전 조회 4,666
11년 전 조회 1,448
11년 전 조회 1,300
11년 전 조회 1,942
11년 전 조회 1,912
11년 전 조회 1,496
11년 전 조회 1,131
11년 전 조회 1,767
11년 전 조회 1,154
11년 전 조회 1,261
11년 전 조회 1,456
11년 전 조회 1,279
11년 전 조회 1,029
11년 전 조회 2,216
11년 전 조회 2,039
11년 전 조회 3,212
11년 전 조회 1,183
11년 전 조회 947
11년 전 조회 1,048
11년 전 조회 2,916
11년 전 조회 1,162
11년 전 조회 1,362
11년 전 조회 890
11년 전 조회 1,664
11년 전 조회 1,655
11년 전 조회 1,058
11년 전 조회 1,239
11년 전 조회 929
11년 전 조회 887
11년 전 조회 1,717
11년 전 조회 1,050
11년 전 조회 945
11년 전 조회 1,061
11년 전 조회 1,221
11년 전 조회 901
11년 전 조회 939
11년 전 조회 1,411
11년 전 조회 980
11년 전 조회 1,436
11년 전 조회 984
11년 전 조회 1,097
11년 전 조회 1,162
11년 전 조회 910
11년 전 조회 942
11년 전 조회 1,164
11년 전 조회 2,094
11년 전 조회 952
11년 전 조회 963
11년 전 조회 891
11년 전 조회 1,320
11년 전 조회 960
11년 전 조회 879
11년 전 조회 1,185
11년 전 조회 1,528
11년 전 조회 1,170
11년 전 조회 1,101
11년 전 조회 1,125
11년 전 조회 1,969
11년 전 조회 1,037
11년 전 조회 1,090
11년 전 조회 2,888
11년 전 조회 2,425
11년 전 조회 1,295
11년 전 조회 1,784
11년 전 조회 1,400
11년 전 조회 1,319
🐛 버그신고