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

· 11년 전 · 3210
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년 전 조회 798
11년 전 조회 2,562
11년 전 조회 2,531
11년 전 조회 2,614
11년 전 조회 1,604
11년 전 조회 2,071
11년 전 조회 1,406
11년 전 조회 1,204
11년 전 조회 1,793
11년 전 조회 1,108
11년 전 조회 4,000
11년 전 조회 3,778
11년 전 조회 1,397
11년 전 조회 2,648
11년 전 조회 1,060
11년 전 조회 1,867
11년 전 조회 3,473
11년 전 조회 3,782
11년 전 조회 4,701
11년 전 조회 1,091
11년 전 조회 1,653
11년 전 조회 3,053
11년 전 조회 1,235
11년 전 조회 1,236
11년 전 조회 1,840
11년 전 조회 1,113
11년 전 조회 2,377
11년 전 조회 1,880
11년 전 조회 3,963
11년 전 조회 2,404
11년 전 조회 4,666
11년 전 조회 1,448
11년 전 조회 1,300
11년 전 조회 1,941
11년 전 조회 1,911
11년 전 조회 1,496
11년 전 조회 1,131
11년 전 조회 1,765
11년 전 조회 1,153
11년 전 조회 1,261
11년 전 조회 1,454
11년 전 조회 1,279
11년 전 조회 1,029
11년 전 조회 2,216
11년 전 조회 2,038
11년 전 조회 3,211
11년 전 조회 1,183
11년 전 조회 947
11년 전 조회 1,048
11년 전 조회 2,916
11년 전 조회 1,161
11년 전 조회 1,361
11년 전 조회 889
11년 전 조회 1,664
11년 전 조회 1,655
11년 전 조회 1,058
11년 전 조회 1,239
11년 전 조회 928
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년 전 조회 982
11년 전 조회 1,097
11년 전 조회 1,162
11년 전 조회 910
11년 전 조회 942
11년 전 조회 1,163
11년 전 조회 2,094
11년 전 조회 951
11년 전 조회 962
11년 전 조회 891
11년 전 조회 1,320
11년 전 조회 960
11년 전 조회 879
11년 전 조회 1,185
11년 전 조회 1,528
11년 전 조회 1,170
11년 전 조회 1,100
11년 전 조회 1,125
11년 전 조회 1,969
11년 전 조회 1,036
11년 전 조회 1,089
11년 전 조회 2,888
11년 전 조회 2,425
11년 전 조회 1,295
11년 전 조회 1,783
11년 전 조회 1,400
11년 전 조회 1,317
🐛 버그신고