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

· 11년 전 · 3059
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,018
11년 전 조회 3,400
11년 전 조회 2,602
11년 전 조회 3,161
11년 전 조회 653
11년 전 조회 2,415
11년 전 조회 2,397
11년 전 조회 2,472
11년 전 조회 1,452
11년 전 조회 1,928
11년 전 조회 1,259
11년 전 조회 1,065
11년 전 조회 1,648
11년 전 조회 978
11년 전 조회 3,850
11년 전 조회 3,628
11년 전 조회 1,254
11년 전 조회 2,502
11년 전 조회 919
11년 전 조회 1,724
11년 전 조회 3,330
11년 전 조회 3,632
11년 전 조회 4,552
11년 전 조회 949
11년 전 조회 1,513
11년 전 조회 2,915
11년 전 조회 1,094
11년 전 조회 1,082
11년 전 조회 1,693
11년 전 조회 951
11년 전 조회 2,230
11년 전 조회 1,737
11년 전 조회 3,811
11년 전 조회 2,271
11년 전 조회 4,519
11년 전 조회 1,298
11년 전 조회 1,156
11년 전 조회 1,799
11년 전 조회 1,764
11년 전 조회 1,339
11년 전 조회 984
11년 전 조회 1,636
11년 전 조회 1,009
11년 전 조회 1,103
11년 전 조회 1,320
11년 전 조회 1,132
11년 전 조회 886
11년 전 조회 2,073
11년 전 조회 1,887
11년 전 조회 3,060
11년 전 조회 1,034
11년 전 조회 799
11년 전 조회 889
11년 전 조회 2,770
11년 전 조회 1,021
11년 전 조회 1,221
11년 전 조회 736
11년 전 조회 1,516
11년 전 조회 1,510
11년 전 조회 918
11년 전 조회 1,108
11년 전 조회 785
11년 전 조회 745
11년 전 조회 1,565
11년 전 조회 902
11년 전 조회 804
11년 전 조회 916
11년 전 조회 1,092
11년 전 조회 751
11년 전 조회 796
11년 전 조회 1,293
11년 전 조회 839
11년 전 조회 1,316
11년 전 조회 836
11년 전 조회 979
11년 전 조회 1,023
11년 전 조회 766
11년 전 조회 793
11년 전 조회 1,031
11년 전 조회 1,963
11년 전 조회 804
11년 전 조회 816
11년 전 조회 750
11년 전 조회 1,176
11년 전 조회 811
11년 전 조회 731
11년 전 조회 1,052
11년 전 조회 1,386
11년 전 조회 1,019
11년 전 조회 949
11년 전 조회 981
11년 전 조회 1,816
11년 전 조회 884
11년 전 조회 942
11년 전 조회 2,724
11년 전 조회 2,272
11년 전 조회 1,148
11년 전 조회 1,641
11년 전 조회 1,250
11년 전 조회 1,181
🐛 버그신고