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

· 11년 전 · 3119
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,078
11년 전 조회 3,454
11년 전 조회 2,659
11년 전 조회 3,211
11년 전 조회 708
11년 전 조회 2,483
11년 전 조회 2,453
11년 전 조회 2,526
11년 전 조회 1,509
11년 전 조회 1,993
11년 전 조회 1,319
11년 전 조회 1,120
11년 전 조회 1,705
11년 전 조회 1,021
11년 전 조회 3,912
11년 전 조회 3,685
11년 전 조회 1,317
11년 전 조회 2,558
11년 전 조회 971
11년 전 조회 1,778
11년 전 조회 3,387
11년 전 조회 3,689
11년 전 조회 4,614
11년 전 조회 1,004
11년 전 조회 1,567
11년 전 조회 2,971
11년 전 조회 1,150
11년 전 조회 1,143
11년 전 조회 1,754
11년 전 조회 1,012
11년 전 조회 2,287
11년 전 조회 1,792
11년 전 조회 3,876
11년 전 조회 2,330
11년 전 조회 4,577
11년 전 조회 1,346
11년 전 조회 1,216
11년 전 조회 1,857
11년 전 조회 1,823
11년 전 조회 1,391
11년 전 조회 1,041
11년 전 조회 1,678
11년 전 조회 1,059
11년 전 조회 1,163
11년 전 조회 1,373
11년 전 조회 1,194
11년 전 조회 941
11년 전 조회 2,130
11년 전 조회 1,954
11년 전 조회 3,120
11년 전 조회 1,090
11년 전 조회 848
11년 전 조회 949
11년 전 조회 2,824
11년 전 조회 1,070
11년 전 조회 1,268
11년 전 조회 791
11년 전 조회 1,573
11년 전 조회 1,559
11년 전 조회 968
11년 전 조회 1,146
11년 전 조회 834
11년 전 조회 790
11년 전 조회 1,619
11년 전 조회 952
11년 전 조회 849
11년 전 조회 966
11년 전 조회 1,128
11년 전 조회 799
11년 전 조회 844
11년 전 조회 1,325
11년 전 조회 886
11년 전 조회 1,351
11년 전 조회 875
11년 전 조회 1,010
11년 전 조회 1,068
11년 전 조회 814
11년 전 조회 840
11년 전 조회 1,066
11년 전 조회 2,001
11년 전 조회 849
11년 전 조회 863
11년 전 조회 794
11년 전 조회 1,217
11년 전 조회 864
11년 전 조회 777
11년 전 조회 1,087
11년 전 조회 1,431
11년 전 조회 1,064
11년 전 조회 988
11년 전 조회 1,034
11년 전 조회 1,868
11년 전 조회 932
11년 전 조회 990
11년 전 조회 2,772
11년 전 조회 2,316
11년 전 조회 1,188
11년 전 조회 1,682
11년 전 조회 1,293
11년 전 조회 1,225
🐛 버그신고