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

· 11년 전 · 3090
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,052
11년 전 조회 3,432
11년 전 조회 2,637
11년 전 조회 3,186
11년 전 조회 684
11년 전 조회 2,458
11년 전 조회 2,431
11년 전 조회 2,500
11년 전 조회 1,489
11년 전 조회 1,967
11년 전 조회 1,290
11년 전 조회 1,096
11년 전 조회 1,680
11년 전 조회 1,001
11년 전 조회 3,886
11년 전 조회 3,659
11년 전 조회 1,293
11년 전 조회 2,532
11년 전 조회 943
11년 전 조회 1,755
11년 전 조회 3,360
11년 전 조회 3,664
11년 전 조회 4,584
11년 전 조회 980
11년 전 조회 1,540
11년 전 조회 2,944
11년 전 조회 1,122
11년 전 조회 1,111
11년 전 조회 1,728
11년 전 조회 984
11년 전 조회 2,261
11년 전 조회 1,763
11년 전 조회 3,847
11년 전 조회 2,300
11년 전 조회 4,546
11년 전 조회 1,324
11년 전 조회 1,188
11년 전 조회 1,830
11년 전 조회 1,795
11년 전 조회 1,368
11년 전 조회 1,014
11년 전 조회 1,656
11년 전 조회 1,036
11년 전 조회 1,135
11년 전 조회 1,347
11년 전 조회 1,164
11년 전 조회 912
11년 전 조회 2,101
11년 전 조회 1,917
11년 전 조회 3,091
11년 전 조회 1,067
11년 전 조회 822
11년 전 조회 920
11년 전 조회 2,795
11년 전 조회 1,049
11년 전 조회 1,242
11년 전 조회 764
11년 전 조회 1,543
11년 전 조회 1,531
11년 전 조회 938
11년 전 조회 1,126
11년 전 조회 808
11년 전 조회 769
11년 전 조회 1,591
11년 전 조회 927
11년 전 조회 823
11년 전 조회 936
11년 전 조회 1,107
11년 전 조회 772
11년 전 조회 816
11년 전 조회 1,306
11년 전 조회 862
11년 전 조회 1,333
11년 전 조회 852
11년 전 조회 992
11년 전 조회 1,043
11년 전 조회 788
11년 전 조회 814
11년 전 조회 1,048
11년 전 조회 1,978
11년 전 조회 825
11년 전 조회 835
11년 전 조회 771
11년 전 조회 1,199
11년 전 조회 836
11년 전 조회 755
11년 전 조회 1,069
11년 전 조회 1,409
11년 전 조회 1,038
11년 전 조회 965
11년 전 조회 1,006
11년 전 조회 1,841
11년 전 조회 910
11년 전 조회 967
11년 전 조회 2,741
11년 전 조회 2,290
11년 전 조회 1,164
11년 전 조회 1,661
11년 전 조회 1,266
11년 전 조회 1,201
🐛 버그신고